我有一个单词列表,说
List<String> words = Arrays.asList("Hello alan i am here where are you"+
"and what are you doing hello are you there");
如何按降序获取列表中重复多次的前七个单词?然后单词应按字母顺序排列。所以上面的输出应该是前七个词
you (3)
are (2)
hello (2)
alan (1)
am (1)
and (1)
doing (1)
我正在使用流 lamda 在 Java 8 中执行此操作。
我正在尝试这种方式。首先对列表进行排序其次获取单词映射及其在单词列表中的单词数。
List<String> sortedWords = Arrays.asList("Hello alan i am here where are you and what are you doing hello you there".split(" "))
.stream().sorted().collect(toList());
Map<String, Long> collect =
sortedWords.stream().collect(groupingBy(Function.identity(), counting()));