Java 8中如何统计List的词频?
List <String> wordsList = Lists.newArrayList("hello", "bye", "ciao", "bye", "ciao");
结果必须是:
{ciao=2, hello=1, bye=2}
最佳答案
我想分享我找到的解决方案,因为一开始我希望使用 map-and-reduce 方法,但它有点不同。
Map<String,Long> collect = wordsList.stream()
.collect( Collectors.groupingBy( Function.identity(), Collectors.counting() ));
或者对于整数值:
Map<String,Integer> collect = wordsList.stream()
.collect( Collectors.groupingBy( Function.identity(), Collectors.summingInt(e -> 1) ));
编辑
我添加了如何按值对 map 进行排序:
LinkedHashMap<String, Long> countByWordSorted = collect.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(v1, v2) -> {
throw new IllegalStateException();
},
LinkedHashMap::new
));
关于java - 词频计数 Java 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29122394/