Stream Gatherers (JEP 473) feedback
Archie Cobbs
archie.cobbs at gmail.com
Fri Sep 20 17:20:24 UTC 2024
Hi Viktor,
I recently encountered a real-world use case for Stream Gatherers and
thought I'd mention it (apologies if you've seen these ideas already).
These might even be worth considering including as standard offerings.
Motivating example: Suppose you have a large list of people sorted by
LastName. Your goal is to print the list ordered by LastName, then by
FirstName. The list is too large to re-sort the whole thing.
Instead, you only need to "fixup" the ordering, by re-sorting just the
groups of people with the same last name.
If you happen to know that there are no more than 100 people with the exact
same last nam, then one option would be a Gatherer that re-sorts a stream,
but only up to some maximum window size (if two elements were out of order
and separated by more than the window size, then sorted output would no
longer be guaranteed).
In the above example, you would set the window size to 100 and it might
look something like this:
listOfPeople.stream() // sorted by lastName only
.gather(Gatherers.windowSort(100,
Comparator.comparing(Person::lastName).thenComparing(Person::firstName)));
Another (probably better) option would be a "secondary sort" Gatherer. This
would take an already sorted stream and then apply a secondary sort. It
would collect items having the same primary sort key (however many there
were), and then re-sort those groups all at once using the secondary key:
listOfPeople.stream() // sorted by lastName only
.gather(Gatherers.secondarySort(Comparator.comparing(Person::lastName),
Comparator.comparing(Person::firstName)));
This kind of thing is common when querying a database that doesn't have the
required composite index corresponding to a desired composite sort, e.g.,
there is an index on LastName and an index on FirstName, but no composite
index on LastName+FirstName, etc.
-Archie
--
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20240920/3a884a4b/attachment-0001.htm>
More information about the core-libs-dev
mailing list