计算单词的频率和java8中列表的返回列表[重复]

问题描述 投票:-5回答:1

这个问题在这里已有答案:

计算句子中单词的频率。

示例:"hey hi hey"

输出:List[List["hey",2],List["hi",1]]

java java-8 java-stream
1个回答
1
投票

如果你改变主意想要一个Map<String, Integer>,这是一种方式(因为List<List<Object>>无论如何都没有意义)

 youList.stream()
        .collect(Collectors.groupingBy(
             Function.identity(),
             Collectors.counting()
 ))

您可以将Collectors.counting()替换为Collectors.summingInt(x -> 1),以便在java-8下进行小改进。

© www.soinside.com 2019 - 2024. All rights reserved.