[英]How to sort TreeMap values in descending order and how to limit the output?
this is my third day with Java (beginner coder in general) and I am finding trouble with getting the desired output I need.这是我使用 Java(一般是初学者)的第三天,我在获得所需的 output 时遇到了麻烦。 I am trying to find the frequency of words occurring in a string or text file.我正在尝试查找字符串或文本文件中出现的单词的频率。 My whole program works so far except I am having difficulty with outputting the result from most frequent words to less;到目前为止,我的整个程序都可以正常工作,除了我很难将最常用单词的结果输出到更少; furthermore how can I limit it to the top x most used words for example.此外,例如,我如何将其限制为最常用的 x 个单词。
Here is my code so far:到目前为止,这是我的代码:
public static void wordOccurrence(String text) {
String[] wordSplit = text.split(" ");
for (int i = 0; i < wordSplit.length; i++) {
Map<String, Integer> occurrence = new TreeMap<>(Collections.reverseOrder());
int Counter = 0;
for (int j = 0; j < wordSplit.length; j++) {
if (wordSplit[i].equals(wordSplit[j])) {
if (j < i)
break;
Counter++;
occurrence.put(wordSplit[j],Counter);
}
}
if (Counter > 1)
System.out.println(occurrence);
}
}
and here is my output which is unordered:{The=2}{that=2}{to=2}{and=5}{for=2}{as=2}这是我的 output 无序:{The=2}{that=2}{to=2}{and=5}{for=2}{as=2}
You are using TreeMap
to sort your entries.您正在使用TreeMap
对条目进行排序。 TreeMap
sorts entries by key, not value. TreeMap
按键而不是值对条目进行排序。
You can use streams
and LinkedHashMap
for that job:您可以将streams
和LinkedHashMap
用于该作业:
public static void wordOccurrence(String text) {
String[] wordSplit = text.split(" ");
Map<String, Long> map = Arrays.stream(wordSplit)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
List<Entry<String, Long>> list = new ArrayList<>(map.entrySet());
list.sort(Entry.comparingByValue(Comparator.reverseOrder()));
Map<String, Long> occurrence = list.stream()
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (s1, s2) -> s1, LinkedHashMap::new));
occurrence.entrySet().forEach(entry -> System.out.println(entry.getKey()+";"+entry.getValue()));
}
Or whithout using List
:或者不使用List
:
public static void wordOccurrence(String text) {
String[] wordSplit = text.split(" ");
Map<String, Long> map = Arrays.stream(wordSplit)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Map<String, Long> occurrence = map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (s1, s2) -> s1, LinkedHashMap::new));
occurrence.entrySet().forEach(entry -> System.out.println(entry.getKey()+";"+entry.getValue()));
}
If you just want the top "n" you can add a line with .limit(n)
:如果您只想要顶部的“n”,您可以添加一行.limit(n)
:
Map<String, Long> occurrence = map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.limit(5)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (s1, s2) -> s1, LinkedHashMap::new));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.