Get the keys with the biggest values from a hashmap?

后端 未结 8 1700
时光取名叫无心
时光取名叫无心 2020-12-06 02:01

I have a HashMap defined like this...

HashMap uniqueNames = new HashMap();

It stor

8条回答
  •  既然无缘
    2020-12-06 02:25

    If you have to use a HashMap, then the simplest way is probabably just to loop through the Map looking for the maximum

    Entry maxEntry = null;
    
    for(Entry entry : uniqueNames.entrySet()) {
        if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
            maxEntry = entry;
        }
    }
    // maxEntry should now contain the maximum,
    

提交回复
热议问题