【发布时间】:2018-03-08 05:19:02
【问题描述】:
有没有办法遍历java中的hashmap,其中键是字符串,值是整数,试图找到出现频率最高的单词
【问题讨论】:
有没有办法遍历java中的hashmap,其中键是字符串,值是整数,试图找到出现频率最高的单词
【问题讨论】:
这里是解决方案。 “试图找到最常出现的词”部分我不清楚
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
【讨论】:
不明白“试图找到出现频率最高的词”。你可以试试这个。
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("k1", 2);
map.put("k3", 3);
map.put("k2", 4);
Set keySet = map.keySet();
Iterator it = keySet.iterator();
while (it.hasNext()) {
String key = (String) it.next();
System.out.println(map.get(key));
}
【讨论】: