<div dir="ltr"><div>Hi Viktor,</div><div><br></div><div>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.<br></div><div><br></div><div>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.</div><div><br></div><div>Instead, you only need to "fixup" the ordering, by re-sorting just the groups of people with the same last name.<br></div><div><br></div><div>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).<br></div><div><br></div><div>In the above example, you would set the window size to 100 and it might look something like this:<br></div><div><br></div><div style="margin-left:40px"><span style="font-family:monospace">listOfPeople.stream()   // sorted by lastName only<br></span></div><div style="margin-left:40px"><span style="font-family:monospace">  .gather(Gatherers.windowSort(100, Comparator.comparing(Person::lastName).thenComparing(Person::firstName)));</span></div><div><br></div><div>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:<br></div><div><br></div><div><div style="margin-left:40px"><span style="font-family:monospace">listOfPeople.stream()</span><span style="font-family:monospace">   // sorted by lastName only</span></div><div style="margin-left:40px"><span style="font-family:monospace">  .gather(Gatherers.secondarySort(Comparator.comparing(Person::lastName), Comparator.comparing(Person::firstName)));</span></div><div><br></div><div>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.<br></div><div><br></div></div><div>-Archie<br></div><div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div></div>