Java – get the maximum key from HashMap?
                                        
                    •
                    Java                                    
                I have a HashMap defined like this
HashMap<String,Integer> uniqueNames = new HashMap<String,Integer>();
It stores a name and the occurrence of that name For example
uniqueNames.put("lastname",42);
How to get the highest name?
For more information, I'm using the "people" binary search tree to store unique names and frequencies in HashMap What I want to do is print the most commonly used last names. Someone told me to use HashMap because I want to store a string and an integer Maybe I should use a class to store names and frequencies? Someone can make some suggestions
Solution
If you have to use a HashMap, the easiest way may be to cycle through the map to find the maximum value
Entry<String,Integer> maxEntry = null;
for(Entry<String,Integer> entry : uniqueNames.entrySet()) {
    if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
        maxEntry = entry;
    }
}
// maxEntry should Now contain the maximum,
                
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        