Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Programming With Streams

Functional Programming With Streams

Functional Programming With Streams

Avatar for Riadh MNASRI

Riadh MNASRI

August 24, 2020

More Decks by Riadh MNASRI

Other Decks in Technology

Transcript

  1. Who am I ? • Riadh MNASRI • Senior Java

    Developer • Senior Kotlin Developer • Interested By : - TDD, BDD, DDD, … - Clean Code - Functional Programming • Twitter : @riadhmnasri
  2. 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
  3. 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
  4. 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);
  5. 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);
  6. 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());
  7. 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()));
  8. 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));
  9. 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()
  10. 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
  11. 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