Speaker: Viktor Klang
See the table of contents for more posts
Oracle has sample code so I didn’t take notes on all the code
General
- Reviewed source, intermediate operations, terminal operations vocabulary
- Imagine if could have any intermediate Stream operations; can grow API
- Features need (collectors don’t meet all needs) – consume/produce ratios, finite/infinite, stateful/stateless, frugal/greedy, sequential/parallelizable, whether to react to end of stream
- Stream gatherers preview in Java 22/23. Released in Java 24
New interface
- Gatherer<T, A, R> – R is what goes to next step
- Supplier<A> initializer()
- Integrator<A, T, R> integrator() – single abstract method boolean integrate(A state, T element, Downstream<R> downstream) – single abstract method – boolean push(R element)
- BinaryOperator<A> combiner()
- BIConsumer<A, Downstream<R>> finisher()
Basic Examples
- Showed code to implemented map()
- Gatherer.of() to create
- Call as .gather(map(i -> I +1)
- Other examples: mapMulti(), limit()
Named Gatherers
- Progression – start as inline code and then refactor to be own class for reuse.
Parallel vs Sequential
- For sequential, start with evaluate() and call in a loop while source.hasNext() and integrator.integrate() returns true
- For parallel, recursively split the upstream until the chunks are small. (Split/fork into distinct parts)
- For takeWhile(), need to deal with short circuiting/infinite streams. Can cancel() or propogate short circuit symbol
Other built in Gatherers
- scan() – kind of like an incremental add/accumulator
- windowFixed() – get immutable list of certain sized optionally keeping last
- mapConcurrent() – specify maximum concurrency level
Other notes
- Can compose
- Stream pipeline – spliterator + Gatherer? + Collector
My take
This is the first time I’ve seen a presentation on this topic. It was great hearing the explanation and seeing a bunch of example. The font for the code was a little smaller than I’d like but I was able to make it out. Only a bit blurry. Most made sense. A few parts I’m going to need to absorb. He did say “it’s a bit tricky” so I don’t feel bad it wasn’t immediately obvious! The diagrams for parallel were helpful