【发布时间】:2017-02-14 04:24:51
【问题描述】:
我有一个单词列表,比如说
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)
我希望在 Java 8 中使用流来执行此操作,lamda。
我正在尝试这种方式。 首先对列表进行排序 其次,获取单词表及其单词列表中的单词数。
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()));
【问题讨论】:
标签: java java-8 java-stream collectors