Is there way to sort map by value inside this collector without creation a new stream? Now it prints: AAS : 5 ABA : 195 ABC : 12 ABE : 52. Desired: ABA : 195 ABE : 52 ... getTrigraphStream(path) returns: HOW THE TRA GRI ONC GRI ONC INS WHE INS WHE
public Map<String, Long> getTrigraphStatisticsMapAlphabet(String path) {
return getTrigraphStream(path)
.collect(groupingByConcurrent(Function.identity(),
ConcurrentSkipListMap::new, counting()));
}
Is there way to sort map by value inside this collector without creation a new stream?
The answer is No. You can only sort by the value of the grouping after you've finished grouping.
Options:
stream()
method after grouping and sort by
value then collect to a map.collectingAndThen
with a finishing function to do the sorting
and then collect to map. i.e.
return getTrigraphStream(path)
.collect(collectingAndThen(groupingByConcurrent(Function.identity(), counting()),
map -> map.entrySet().stream().sorted(....).collect(...)));
By the way, what's the problem with creating a new stream? it's a cheap operation.