Java: Sort a HashMap by Value 

Joined:
04/09/2007
Posts:
753

September 25, 2008 02:56:24    Last update: September 25, 2008 03:00:10
When you use a TreeMap, the entries in the Map is sorted by the keys.

This following code outputs the elements of the map sorted by value.
import java.util.*;

@SuppressWarnings("unchecked") // for JDK 1.5 and above
public class HashMapSort {
    public static void main(String[] args) {
        Map m = new HashMap();
        m.put("a", "some");
        m.put("b", "random");
        m.put("c", "words");
        m.put("d", "to");
        m.put("e", "be");
        m.put("f", "sorted");
        m.put("g", "by");
        m.put("h", "value");
        for (Iterator i = sortByValue(m).iterator(); i.hasNext(); ) {
            String key = (String) i.next();
            System.out.printf("key: %s, value: %s\n", key, m.get(key));
        }
    }

    public static List sortByValue(final Map m) {
        List keys = new ArrayList();
        keys.addAll(m.keySet());
        Collections.sort(keys, new Comparator() {
            public int compare(Object o1, Object o2) {
                Object v1 = m.get(o1);
                Object v2 = m.get(o2);
                if (v1 == null) {
                    return (v2 == null) ? 0 : 1;
                }
                else if (v1 instanceof Comparable) {
                    return ((Comparable) v1).compareTo(v2);
                }
                else {
                    return 0;
                }
            }
        });
        return keys;
    }
}


The output is:
key: e, value: be
key: g, value: by
key: b, value: random
key: a, value: some
key: f, value: sorted
key: d, value: to
key: h, value: value
key: c, value: words
Share |
| Comment  | Tags
2 comments