所以这个程序给出了每个单词的出现次数。我需要制作一个列表,用这些数据按升序对这些单词进行排序。
import java.util.*;
public class A1E5{
public static void main(String[] args) {
// Set text in a string
String text = "Good morning. Have a good class. " +
"Have a good visit. Have fun!";
// Create a TreeMap to hold words as key and count as value
Map<String, Integer> map = new TreeMap<>();
String[] words = text.split("[\\s+\\p{P}]");
for (int i = 0; i < words.length; i++) {
String key = words[i].toLowerCase();
if (key.length() > 0) {
if (!map.containsKey(key)) {
map.put(key, 1);
}
else {
int value = map.get(key);
value++;
map.put(key, value);
}
}
}
// Display key and value for each entry
map.forEach((k, v) -> System.out.println(k + "\t" + v));
}
}
答案 0 :(得分:0)
实现Comparator接口并覆盖其在java中的compare方法。 在set中获取map.entrySet(),将其转换为list(我们已将set转换为list,因为Collections的sort方法只能接受列表类型作为参数)。 调用Collections.sort并传递列表[即listOfentrySet]作为参数。 Collections.sort在内部调用Arrays.sort,Arrays.Sort()在内部调用Merge Sort.Merge排序调用重写Comparator接口的比较方法,用于比较键。最后listOfentrySet将包含基于键排序的条目(键值)对的java。
这是一个简单的谷歌搜索,但在这里你是:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
class SortByKeyAscending implements Comparator<Map.Entry<Integer, Integer>>{
@Override
public int compare( Map.Entry<Integer,Integer> entry1, Map.Entry<Integer,Integer> entry2){
return (entry1.getKey()).compareTo( entry2.getKey() );
}
}
public class SortMapByKeyAscendingExample {
public static void main(String...a){
Map<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
map.put(4, 1);
map.put(2, 1);
map.put(3, 1);
map.put(5, 1);
Set<Entry<Integer, Integer>> entrySet = map.entrySet();
List<Entry<Integer, Integer>> listOfentrySet = new ArrayList<Entry<Integer, Integer>>(entrySet);
System.out.print("Before sorting by key : ");
for(Map.Entry<Integer, Integer> entry:listOfentrySet){
System.out.print(entry.getKey()+"="+entry.getValue()+" ");
}
Collections.sort(listOfentrySet, new SortByKeyAscending());
System.out.print("\nAfter sorting by key(ascending): ");
for(Map.Entry<Integer, Integer> entry:listOfentrySet)
System.out.print(entry.getKey()+"="+entry.getValue()+" ");
}
}
输出为(键值)
在排序之前:4 = 1 2 = 1 3 = 1 5 = 1
排序后:2 = 1 3 = 1 4 = 1 5 = 1
答案 1 :(得分:0)
如果我正确理解了你的要求,因为你不能让TreeMap本身对你想要的值进行排序:
List<String> wordsList = new ArrayList<String>();
map.forEach((k, v) -> wordsList.add(k));
System.out.println(Arrays.toString(wordsList.toArray()));
<强>输出:强>
[a, class, fun, good, have, morning, visit]
或者这个:
List<String> wordsList = new ArrayList<String>();
for(Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
for(int i = 0 ; i <= entry.getValue(); i++) {
wordsList.add(key);
}
}
System.out.println(Arrays.toString(wordsList.toArray()));
<强>输出:强>
[a, a, a, class, class, fun, fun, good, good, good, good, have, have, have, have, morning, morning, visit, visit]
答案 2 :(得分:0)
如果您尝试打印按计数排序的结果,则可以执行以下操作:
map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.forEach(e -> System.out.println(e.getKey() + "\t" + e.getValue()));
您还可以首先使用流来制作频率图:
Pattern.compile("[\\s+\\p{P}]")
.splitAsStream(text)
.map(String::toLowerCase)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.forEach(e -> System.out.println(e.getKey() + "\t" + e.getValue()));