SlideShare a Scribd company logo
Functional Programming with Streams
Prepared by: Riadh MNASRI
Who am I ?
● Riadh MNASRI
● Senior Java Developer
● Senior Kotlin Developer
● Interested By :
- TDD, BDD, DDD, …
- Clean Code
- Functional Programming
● Twitter : @riadhmnasri
Agenda
● Whats Streams are ?
● Stream operations
● How to create streams
● Applying different operations with streams
● Collecting data with streams
● Grouping
● Partitioning
● Finding and matching
● Conclusion
● References
What Streams are ?
● A stream is a sequence of data elements supporting
sequential and parallel aggregate operations.
● Consumes elements from its data source
● Focus on aggregagte computations
● Have no storage
● Can represent a sequence of infinite elements
● Support functional programming
● Stream are not reusable
Stream operations
● Intermediate operations: lazy operations
● Terminal operations: eager operations
How to create streams ?
● Streams from values:
Stream<String> stream = Stream.of("Hello");
● Empty streams : Stream.empty();
● Streams from functions:
Stream.iterate(1L, n -> n + 1);
Stream.generate(Math::random);
●
Streams from arrays: Arrays.stream(new int[]{1, 2, 3});
● Streams from collections: list().stream()
● Streams from files: Files.lines(path)
● Streams from other sources:
Pattern.compile(",").splitAsStream(str);
Applying different operations with streams
● Debuging stream: peek()
● Applying foreach: forEach(Consumer<? super T>
action)
● Applying map (one to one mapping): <R> Stream<R>
map(Function<? super T,? extends R> mapper)
● Flatenning (one to many mapping): flatMap(n ->
Stream.of(n, n * n))
● Filtering: filter(p -> p.getIncome() > 5000.0)
● Reducing: reduce(0.0, Double::sum);
Collecting data with streams
● <R> R collect(Supplier<R> supplier, BiConsumer<R,? super
T> accumulator, BiConsumer<R,R> combiner)
Example: collect(ArrayList::new,
ArrayList::add, ArrayList::addAll)
● <R,A> R collect(Collector<? super T,A,R> collector)
Example: collect(Collectors.toList());
Grouping
● In the most generic version, the groupingBy() method takes
two parameters:
- A classifier that is a function to generate the keys in the
map.
- A collector that performs a reduction operation on the
values associated with each key:
collect(Collectors.groupingBy(Person::getGen
der, Collectors.counting()));
Partitioning
● Partitioning data is a special case of grouping data
● Grouping data is based on the keys returned from a function
● Partitioning groups data into two groups:
- For one group a condition is true
- For the other, the same condition is false
● The partitioning condition is specified using a Predicate:
books
.stream()
.collect(Collectors.partitioningBy(Book::isProgramming));
Finding and matching
● boolean allMatch(Predicate<? super T>
predicate)
● boolean anyMatch(Predicate<? super T>
predicate)
● boolean noneMatch(Predicate<? super T>
predicate)
● Optional<T> findAny()
● Optional<T> findFirst()
Live coding
Conclusion
● A stream is a sequence of data elements supporting
sequential and parallel aggregate operations.
● Streams are connected through operations forming a
pipeline.
● Streams support two types of operations: intermediate and
terminal operations.
● Streams allows making aggregation operations on
collections in functional programming style
References
● Book - Beginning Java 8 langugage features
● https://stackify.com/streams-guide-java-8/
● Examples are available in my personal github :
https://github.com/riadh-mnasri/bbl-java-stream-api
Functional programming with streams

More Related Content

Similar to Functional programming with streams (20)

PDF
OpenTSDB 2.0
HBaseCon
 
PDF
RMLL 2013 - Synchronize OpenLDAP and Active Directory with LSC
Clément OUDOT
 
PPTX
Redux
Anurag Chitti
 
PPTX
Introduction To MongoDB
ElieHannouch
 
PDF
Druid
Dori Waldman
 
PDF
Solr Power FTW: Powering NoSQL the World Over
Alex Pinkin
 
PDF
Anatomy of Data Frame API : A deep dive into Spark Data Frame API
datamantra
 
PDF
Redis Day TLV 2018 - RediSearch Aggregations
Redis Labs
 
PDF
How to build TiDB
PingCAP
 
PDF
Social Data and Log Analysis Using MongoDB
Takahiro Inoue
 
PDF
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Qbeast
 
ODP
Aggregation Framework in MongoDB Overview Part-1
Anuj Jain
 
PDF
Netflix Machine Learning Infra for Recommendations - 2018
Karthik Murugesan
 
PDF
ML Infra for Netflix Recommendations - AI NEXTCon talk
Faisal Siddiqi
 
PDF
Native Container Monitoring
Anushree Narasimha
 
PDF
Native container monitoring
Rohit Jnagal
 
PDF
Large volume data analysis on the Typesafe Reactive Platform - Big Data Scala...
Martin Zapletal
 
PDF
Precog & MongoDB User Group: Skyrocket Your Analytics
MongoDB
 
PDF
Streaming Data from Cassandra into Kafka
Abrar Sheikh
 
PDF
Introduction to InfluxDB, an Open Source Distributed Time Series Database by ...
Hakka Labs
 
OpenTSDB 2.0
HBaseCon
 
RMLL 2013 - Synchronize OpenLDAP and Active Directory with LSC
Clément OUDOT
 
Introduction To MongoDB
ElieHannouch
 
Solr Power FTW: Powering NoSQL the World Over
Alex Pinkin
 
Anatomy of Data Frame API : A deep dive into Spark Data Frame API
datamantra
 
Redis Day TLV 2018 - RediSearch Aggregations
Redis Labs
 
How to build TiDB
PingCAP
 
Social Data and Log Analysis Using MongoDB
Takahiro Inoue
 
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Qbeast
 
Aggregation Framework in MongoDB Overview Part-1
Anuj Jain
 
Netflix Machine Learning Infra for Recommendations - 2018
Karthik Murugesan
 
ML Infra for Netflix Recommendations - AI NEXTCon talk
Faisal Siddiqi
 
Native Container Monitoring
Anushree Narasimha
 
Native container monitoring
Rohit Jnagal
 
Large volume data analysis on the Typesafe Reactive Platform - Big Data Scala...
Martin Zapletal
 
Precog & MongoDB User Group: Skyrocket Your Analytics
MongoDB
 
Streaming Data from Cassandra into Kafka
Abrar Sheikh
 
Introduction to InfluxDB, an Open Source Distributed Time Series Database by ...
Hakka Labs
 

More from Riadh MNASRI (7)

PDF
Programmation Fonctionnelle avec Kotlin
Riadh MNASRI
 
PDF
Ecrire son 1er DSL avec Kotlin
Riadh MNASRI
 
PPTX
Kotlin arrowkt
Riadh MNASRI
 
PPTX
BBL Reactive Programming
Riadh MNASRI
 
PPTX
Discover Micronaut
Riadh MNASRI
 
PDF
Architectures microservices
Riadh MNASRI
 
PPT
Git l'essentiel
Riadh MNASRI
 
Programmation Fonctionnelle avec Kotlin
Riadh MNASRI
 
Ecrire son 1er DSL avec Kotlin
Riadh MNASRI
 
Kotlin arrowkt
Riadh MNASRI
 
BBL Reactive Programming
Riadh MNASRI
 
Discover Micronaut
Riadh MNASRI
 
Architectures microservices
Riadh MNASRI
 
Git l'essentiel
Riadh MNASRI
 
Ad

Recently uploaded (20)

PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Digital Circuits, important subject in CS
contactparinay1
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Ad

Functional programming with streams

  • 1. Functional Programming with Streams Prepared by: Riadh MNASRI
  • 2. Who am I ? ● Riadh MNASRI ● Senior Java Developer ● Senior Kotlin Developer ● Interested By : - TDD, BDD, DDD, … - Clean Code - Functional Programming ● Twitter : @riadhmnasri
  • 3. Agenda ● Whats Streams are ? ● Stream operations ● How to create streams ● Applying different operations with streams ● Collecting data with streams ● Grouping ● Partitioning ● Finding and matching ● Conclusion ● References
  • 4. What Streams are ? ● A stream is a sequence of data elements supporting sequential and parallel aggregate operations. ● Consumes elements from its data source ● Focus on aggregagte computations ● Have no storage ● Can represent a sequence of infinite elements ● Support functional programming ● Stream are not reusable
  • 5. Stream operations ● Intermediate operations: lazy operations ● Terminal operations: eager operations
  • 6. How to create streams ? ● Streams from values: Stream<String> stream = Stream.of("Hello"); ● Empty streams : Stream.empty(); ● Streams from functions: Stream.iterate(1L, n -> n + 1); Stream.generate(Math::random); ● Streams from arrays: Arrays.stream(new int[]{1, 2, 3}); ● Streams from collections: list().stream() ● Streams from files: Files.lines(path) ● Streams from other sources: Pattern.compile(",").splitAsStream(str);
  • 7. Applying different operations with streams ● Debuging stream: peek() ● Applying foreach: forEach(Consumer<? super T> action) ● Applying map (one to one mapping): <R> Stream<R> map(Function<? super T,? extends R> mapper) ● Flatenning (one to many mapping): flatMap(n -> Stream.of(n, n * n)) ● Filtering: filter(p -> p.getIncome() > 5000.0) ● Reducing: reduce(0.0, Double::sum);
  • 8. Collecting data with streams ● <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner) Example: collect(ArrayList::new, ArrayList::add, ArrayList::addAll) ● <R,A> R collect(Collector<? super T,A,R> collector) Example: collect(Collectors.toList());
  • 9. Grouping ● In the most generic version, the groupingBy() method takes two parameters: - A classifier that is a function to generate the keys in the map. - A collector that performs a reduction operation on the values associated with each key: collect(Collectors.groupingBy(Person::getGen der, Collectors.counting()));
  • 10. Partitioning ● Partitioning data is a special case of grouping data ● Grouping data is based on the keys returned from a function ● Partitioning groups data into two groups: - For one group a condition is true - For the other, the same condition is false ● The partitioning condition is specified using a Predicate: books .stream() .collect(Collectors.partitioningBy(Book::isProgramming));
  • 11. Finding and matching ● boolean allMatch(Predicate<? super T> predicate) ● boolean anyMatch(Predicate<? super T> predicate) ● boolean noneMatch(Predicate<? super T> predicate) ● Optional<T> findAny() ● Optional<T> findFirst()
  • 13. Conclusion ● A stream is a sequence of data elements supporting sequential and parallel aggregate operations. ● Streams are connected through operations forming a pipeline. ● Streams support two types of operations: intermediate and terminal operations. ● Streams allows making aggregation operations on collections in functional programming style
  • 14. References ● Book - Beginning Java 8 langugage features ● https://stackify.com/streams-guide-java-8/ ● Examples are available in my personal github : https://github.com/riadh-mnasri/bbl-java-stream-api