how to find key that has max value in java hashmap code example
Example: find the greatest number in hashmap
public class NewClass4 {
public static void main(String[] args)
{
HashMap<Integer,Integer>map=new HashMap<Integer, Integer>();
map.put(1, 50);
map.put(2, 60);
map.put(3, 30);
map.put(4, 60);
map.put(5, 60);
int maxValueInMap=(Collections.max(map.values()));
for (Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue()==maxValueInMap) {
System.out.println(entry.getKey());
}
}
}
}