orderByFrequency()
Emit elements in the input stream ordered by the specified frequency.
Implementation Notes
This implementation reads the entire stream before emitting any results making it inappropriate for infinite streams. Since this Gatherer
is not order dependent it has a parallel-capable implementation. All results are wrapped in a WithCount
record.
Signature
orderByFrequency(Frequency order)
order
- EitherAscending
orDescending
Examples
Order elements by frequency, ascending
Stream
.of("A", "A", "A", "B", "B", "B", "B", "C", "C");
.gather(Gatherers4j.orderByFrequency(Frequency.Ascending))
.toList();
// [ WithCount("C", 2), WithCount("A", 3), WithCount("B", 4) ]
Order elements by frequency, descending
Stream
.of("A", "A", "A", "B", "B", "B", "B", "C", "C");
.gather(Gatherers4j.orderByFrequency(Frequency.Descending))
.toList();
// [ WithCount("B", 4), WithCount("A", 3), WithCount("C", 2) ]