Is there a way to iterate through a hashmap in java where the key is a String and a value Is an Integer trying to find the most frequent word that comes up
Asked
Active
Viewed 1,339 times
2 Answers
0
Here is the solution. "trying to find the most frequent word that comes up" part is not clear to me
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());
}

spandey
- 1,034
- 1
- 15
- 30
0
Did not understood 'trying to find the most frequent word that comes up'.You can try this.
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));
}

Manish
- 31
- 3
-
why iterator if you can run for loop on Set – spandey Mar 08 '18 at 11:33