这个问题在这里已有答案:
计算句子中单词的频率。
示例:"hey hi hey"
输出:List[List["hey",2],List["hi",1]]
如果你改变主意想要一个Map<String, Integer>
,这是一种方式(因为List<List<Object>>
无论如何都没有意义)
youList.stream()
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()
))
您可以将Collectors.counting()
替换为Collectors.summingInt(x -> 1)
,以便在java-8下进行小改进。