Sunday, March 17, 2019

Get key with maximum value in hashmap in java

static String findMax(HashMap<String,Integer> hm){
    Map.Entry<String,Integer> maxEntry = null;
    for(Map.Entry<String ,Integer>entry: hm.entrySet()){
        if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) >= 0)
        {
            maxEntry = entry;
        }


    }
    return maxEntry.getKey();

No comments:

Post a Comment