When to Stream, Collect, and Gather

.jpg

Slide navigation: Forward with space bar, , or PgDn. Backwards with or PgUp.

About the Speaker

.jpg

Why Use Streams?

Why Not Always Streams?

  static int numberOfBrickWithChildrenWithAtLeastTwoParents(String input) {
    var grid = new HashMap<Pos, Level>();
    return input.lines()
        .map(l -> {
          var array = Arrays.stream(l.split(",|~")).mapToInt(Integer::parseInt).toArray();
          return new Brick(array[0], array[3], array[1], array[4], array[2], array[5]);
        })
        .collect(groupingBy(Brick::z1, TreeMap::new, toList()))
        .values().stream()
        .flatMap(List::stream)
        .map(brick -> {
          var onTop = brick.positions()
              .map(pos -> grid.getOrDefault(pos, new Level(null, 0)))
              .collect(groupingBy(Level::z, TreeMap::new, flatMapping(l -> Stream.ofNullable(l.brick), toSet())))
              .lastEntry();
          var level =  new Level(brick, 1 + brick.z2 - brick.z1 + onTop.getKey());
          brick.positions().forEach(pos -> grid.put(pos, level));
          return new Pair<>(brick, onTop.getValue());
        })
        .collect(teeing(
            toMap(Pair::u, pair -> pair.v().size()),
            flatMapping(pair -> pair.v().stream().map(b -> new Pair<>(b, pair.u())),
                groupingBy(Pair::u, mapping(Pair::v, toList()))),
            (parentMap, childrenMap) ->
                (int) parentMap.keySet().stream()
                    .filter(parent -> childrenMap.getOrDefault(parent, List.of()).stream()
                        .allMatch(child -> parentMap.get(child) > 1))
                    .count()
        ));
  }
Source, Advent of Code 2023 Day 22

Max and Min

.jpeg

Finding, Counting, Collecting Matches

.jpeg

Nothing to See Here...

.jpeg

Method Expressions

For Each

.jpg

Streams are Everywhere in the Java API

.jpg

Complex Lambdas

Nested Streams

.jpg

Ad-Hoc Records

.png

Collectors

.jpg

Counting Groups

Gatherers

Checked Exceptions

Checked Exceptions

Benchmarking

.jpeg

Memory Consumption

.png

Concurrency

.jpg

Blocking Tasks

Case Study: Phone Mnemonics

.jpg

The End

.jpeg

Questions?

.jpg