Sorting a HashMap based on Value then Key? [duplicate]
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?
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>());
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
.
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.");
}
}
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.
精彩评论