amaembo / streamex

Enhancing Java Stream API
Apache License 2.0
2.18k stars 250 forks source link

Simplify distinct operator if follows the sorted operator #227

Closed numeralnathan closed 4 years ago

numeralnathan commented 4 years ago

Let's say we have the following stream pattern...

...
sorted().
distinct().
...

The distinct operator can be simplified. Instead of putting all of the elements into a Set, the distinct operator only needs to compare to the previous element.

This will reduce the memory usage from O(n) to O(1).

amaembo commented 4 years ago

I believe, it's already done in JDK, and we delegate to JDK streams. See https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/stream/DistinctOps.java#L124