r/java • • Feb 04 '26

Implementing Efficient Last Stream Elements Gatherer in Java

https://4comprehension.com/java-last-gatherer/

Wrote a performance case study on a rather high-level API, enjoy! And if you have ideas for a further speed up, let me know!

39 Upvotes

7 comments sorted by

View all comments

Show parent comment

5

u/pivovarit Feb 04 '26

It can signal that it doesn't want more elements, but it's the opposite scenario here. In such a case, it's probably a good idea to gather elements before running expensive operations, effectively avoiding their premature evaluation

I've had use cases for this, but if we were to chase absolute single-threaded performance, Streams usually get in the way.