有没有办法在不创建新流的情况下按此收集器内的值对地图进行排序?现在打印: AAS :5 ABA : 195 ABC : 12 ABE : 52。期望: ABA : 195 ABE : 52 .getTrigraphStream(路径)返回: TRA、ONC、ONC、WHE、WHE、WHE等
public Map<String, Long> getTrigraphStatisticsMapAlphabet(String path) {
return getTrigraphStream(path)
.collect(groupingByConcurrent(Function.identity(),
ConcurrentSkipListMap::new, counting()));
}发布于 2018-11-20 20:35:41
有没有办法在不创建新流的情况下按此收集器内的值对地图进行排序?
答案是否定的。只有在完成分组后,才能根据分组的值进行排序。
选项:
stream()方法,然后将其收集到映射中。collectingAndThen进行排序,然后收集到地图。即
return getTrigraphStream(path)
.collect(collectingAndThen(groupingByConcurrent(Function.identity(), counting()),
map -> map.entrySet().stream().sorted(....).collect(...)));顺便问一下,创建一个新流有什么问题?这是个廉价的行动。
https://stackoverflow.com/questions/53399973
复制相似问题