Get the keys with the biggest values from a hashmap?

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

I have a HashMap defined like this...

HashMap uniqueNames = new HashMap();

It stor

8条回答
  •  余生分开走
    2020-12-06 02:22

    List list= new ArrayList();
        HashMap map=new HashMap();
    
        for(String string: list)
        {
         if(map.containsKey(string))
         {
             map.put(string, map.get(string)+1);
         }
         else {
            map.put(string, 1);
        }
        }
    
    
        Entry maxEntry = null;
    
        for(Entry entry : map.entrySet()) {
            if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
                maxEntry = entry;
            }
        }
    

提交回复
热议问题