find the greatest number in java hashmap

htt‮w//:sp‬ww.lautturi.com
find the greatest number in java hashmap
/**
 * @author lautturi.com
 * Java example: Find the key associated with the maximum value in java hashmap
 */

import java.util.*;
import java.util.Map.Entry;

public class Lautturi {

	public static void main(String[] args) {

		HashMap<Integer,Integer>map=new HashMap<Integer, Integer>();
        map.put(1, 12);
        map.put(2, 35);
        map.put(3, 57);
        map.put(4, 16);
        map.put(5, 5);
        int maxValueInMap=(Collections.max(map.values()));  // This will return max value in the Hashmap
        for (Entry<Integer, Integer> entry : map.entrySet()) {  // Itrate through hashmap
            if (entry.getValue()==maxValueInMap) {
                System.out.println(entry.getKey());     // Print the key with max value
            }
        }
        
	}
}
Created Time:2017-09-05 21:05:16  Author:lautturi