KEMBAR78
Functional programming with streams | PDF
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

Functional programming with streams

  • 1.
    Functional Programming withStreams 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 Streamsare ? ● 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 ● Intermediateoperations: lazy operations ● Terminal operations: eager operations
  • 6.
    How to createstreams ? ● 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 operationswith 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 withstreams ● <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 themost 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 datais 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()
  • 12.
  • 13.
    Conclusion ● A streamis 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