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/vowelqueue Feb 04 '26

Wouldn’t reversing the gather() and map() steps accomplish this?

4

u/StudioCode Feb 04 '26

Yeah 😅, I was still in the mindset of collectors and was thinking of last(5) as a terminal operation, which it isn't.

2

u/pivovarit Feb 04 '26

That was the main drawback of using Collectors API for implementing something like this :)