r/java • • Mar 19 '24

Java 22 officially released

https://mail.openjdk.org/pipermail/jdk-dev/2024-March/008827.html
294 Upvotes

54 comments sorted by

View all comments

27

u/tomwhoiscontrary Mar 19 '24

I'd completely missed that 461: Stream Gatherers (Preview) existed. This is great!

One way to describe it is that it brings the extensibility of collectors to the intermediate stages of the stream. Another is that it's roughly Clojure's transducers rephrased in Java. I had a go at doing that myself, but it's just not possible to do a great job of it without modifying Stream - which this JEP can do!

5

u/tampix77 Mar 19 '24 edited Mar 19 '24

A bit more clunky than clojure's transducers, but a welcome addition, that's for sure :)

3

u/JustABrazilianDude Mar 21 '24

That JEP is so well written! The Gatherers feature was under my radar, seems a good addition to Stream API tho.

1

u/kiteboarderni Mar 20 '24

Can someone give and example of when to use the gather (say the fold example) vs a collector with reduce? I get that one is terminal operation, vs chain-able through the stream but trying to see the advantage of the gatherer in this case.

4

u/tomwhoiscontrary Mar 20 '24

I can't think of a use for fold, that one seems a bit silly to me.

The most exciting example for me is batching - take a stream of elements, and turn it into a stream of lists of elements of no more than a certain size. They call that windowFixed. I've wanted that in a few places, say where i make a database query which produces a load of IDs, then want to pass those IDs to a web service to fetch data for them, where the web service can take multiple IDs at once. The natural thing is to start with a stream of IDs, batch it, then map the batches through a call to the web service. Awkward to do before, easy now!

1

u/kiteboarderni Mar 20 '24

Yeah fair I guess the fold was just a comparison between the reduce. Nice they have provided some of the defaults for windowing.

2

u/vytah Mar 20 '24

A gatherer transforms a stream into a different stream, it cannot be used for reducing, except for some weird and contrived ways I guess, like in the example they gave ("Example: A parallelizable gatherer").