Sorting a HashMap based on Value then Key? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-11-27 18:39:49

问题


Possible Duplicate:
How to sort a Map<Key, Value> on the values in Java?

I have a HashMap of the type:

HashMap<String, Integer> h = new HashMap<String, Integer>();

The HashMap contains a list of Strings and the Integer is a counter for the number of times that String has been found. What I would like to be able to do is sort the HashMap based on the Integers, then on the alphabetical order of the Strings.

At the moment I am keeping a record of the largest occurrence of a word (variable named max) and displaying the values as follows:

public void print(){
    while(max > 0){
       for (String key : h.keySet()){
           if(h.get(key) == max){
               System.out.println(key + " " + h.get(key));
           }
       }
       max--;
    }
}

Which doesn't sort the values alphabetically, also it accesses the HashMap max*h(size) times.

What is the better solution?


回答1:


Here's a Comparator that sorts Map.Entry objects with Comparable keys and values:

public class ValueThenKeyComparator<K extends Comparable<? super K>,
                                    V extends Comparable<? super V>>
    implements Comparator<Map.Entry<K, V>> {

    public int compare(Map.Entry<K, V> a, Map.Entry<K, V> b) {
        int cmp1 = a.getValue().compareTo(b.getValue());
        if (cmp1 != 0) {
            return cmp1;
        } else {
            return a.getKey().compareTo(b.getKey());
        }
    }

}

You'd put all of the map entries into a list and then sort that:

List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(h.entrySet());
Collections.sort(list, new ValueThenKeyComparator<String, Integer>());



回答2:


Look at Google Guava libraries. It has a Multiset which does the calculation for you and then you have Ordering class that simplifies sorting.

All you need to do is to populate Multiset with your strings. It will maintain the frequency for you. Then you can sort on those strings using Ordering.




回答3:


Probably not the most elegant solution, but how about this?

//TreeSet with reversed natural ordering (big integers first)
Map<Integer, Set<String>> h = 
     new TreeMap<Integer, Set<String>>(Collections.reverseOrder());
//and use TreeSet for the set...
// ...    
// 
for(Map.Entry<Integer,Set<String>> entry : h.entrySet()){
    for(String str : entry.getValue()){
        System.out.println(str + " has occured " + entry.getKey() + " times.");
    }
}



回答4:


you can use SortedMap interface to sort your HashMap. It's very easy - Automatic sorting. Refer to http://java.sun.com/j2se/1.4.2/docs/api/java/util/SortedMap.html. I didn't include any code here, but if you need, just add a comment. I'll give you a sample code.



来源:https://stackoverflow.com/questions/3074154/sorting-a-hashmap-based-on-value-then-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!