Java 8 - 单词计数然后以desc顺序排列 [英] Java 8 - Count of words and then arrange in desc order

查看:108
本文介绍了Java 8 - 单词计数然后以desc顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单词列表,比如说

  List< String> words = Arrays.asList(Hello alan我在这里你在哪里+ 
你在做什么打招呼你在那里);

如何按降序排列列表中重复多次的前七个单词?然后单个输入词应按字母顺序排列。所以上面的输出应该是前七个单词

 你(3)
是(2)
hello(2)
alan(1)
am(1)
和(1)
做(1)

我在Java 8中使用流,lamda这样做。



我正在尝试这条路。
首先对列表进行排序
第二次获取单词的地图及其单词列表中的单词数量。

 列表与LT;字符串> sortedWords = Arrays.asList(Hello alan我在这里你在哪里,你在做什么打招呼.split())
.stream()。sorted()。collect(toList()) ;

Map< String,Long> collect =
sortedWords.stream()。collect(groupingBy(Function.identity(),counting()));


解决方案

最困难的部分是排序。由于您只想保留结果中的7个第一个元素,并且您希望按其值对Map进行排序,我们需要创建一个包含所有结果的Map,对其进行排序,然后保留7个结果。



在下面的代码中,每个单词都是低位的,并按自己分组,计算出现的次数。然后,我们需要对这个映射进行排序,以便我们在条目上创建一个Stream,根据值(按降序排序)然后根据键对它们进行排序。保留7个第一个元素,映射到它们的键(对应于单词)并收集到列表中,从而保持相遇顺序。

  public static void main(String [] args){
String sentence =你好阿兰我在这里你在哪里,你在做什么你好你在那里;
List< String> words = Arrays.asList(sentence.split());

List< String> result =
words.stream()
.map(String :: toLowerCase)
.collect(groupingBy(identity(),counting()))
.entrySet()。 stream()
.sorted(Map.Entry。< String,Long> comparisonByValue(reverseOrder())。thenComparing(Map.Entry.comparingByKey()))
.limit(7)
.map(Map.Entry :: getKey)
.collect(toList());

System.out.println(result);
}

输出:

  [是,你,你好,阿兰,上午,做什么] 

请注意,您在想要的输出中犯了一个错误:are实际上出现了3次,如you所以它应该在之前

注意:这段代码假定有很多静态导入,即:

  import static java.util.Comparator.reverseOrder; 
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;


I have a list of words, say

List<String> words = Arrays.asList("Hello alan i am here where are you"+  
  "and what are you doing hello are you there");

How can I get the top seven words which are repeated more than once in the list in descending order? And then the single entry words should be arranged in alphabetical order. So the output for the above should be those top seven words

you (3)
are (2)
hello (2)
alan (1)
am (1)
and (1)
doing (1)

I am looking this to do in Java 8 using streams, lamda.

I am trying in this way. First sort the list Second get the map of words with its count of words in the list of words.

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()));

解决方案

The most difficult part is the sorting. Since you want to keep only the 7 first elements from the result and you want to sort the Map by its values, we need to create a Map of all the result, sort it and then keep the 7 results.

In the following code, every word is lower-cased and grouped by themselves, counting the number of occurences. Then, we need to sort this map so we create a Stream over the entries, sort them according to the values (in descending order) and then according to the keys. The 7 first elements are retained, mapped to their key (which correspond to the word) and collected into a List, thus keeping encounter order.

public static void main(String[] args) {
    String sentence = "Hello alan i am here where are you and what are you doing hello are you there";
    List<String> words = Arrays.asList(sentence.split(" "));

    List<String> result = 
            words.stream()
                 .map(String::toLowerCase)
                 .collect(groupingBy(identity(), counting()))
                 .entrySet().stream()
                 .sorted(Map.Entry.<String, Long> comparingByValue(reverseOrder()).thenComparing(Map.Entry.comparingByKey()))
                 .limit(7)
                 .map(Map.Entry::getKey)
                 .collect(toList());

    System.out.println(result);
}

Output:

[are, you, hello, alan, am, and, doing]

Note that you made a mistake in your wanted output: "are" actually appears 3 times like "you" so it should be before

NB: this code assumes a lot of static imports, namely:

import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

这篇关于Java 8 - 单词计数然后以desc顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆