Java Data Structure How to - Sort a Map








Question

We would like to know how to sort a Map.

Answer

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/*  w  ww. ja  va  2 s.com*/
class MyComparator implements Comparator<Map.Entry> {
  public int compare(Entry o1, Entry o2) {
    Double valueOne = (Double) o1.getValue();
    Double valueTwo = (Double) o2.getValue();
    return (int) Math.signum(valueOne.compareTo(valueTwo));
  }
}
public class Main {
  public static void main(String[] args) {
    TreeMap tm = new TreeMap();
    tm.put(1, new Double(344.34));
    tm.put(0, new Double(123.22));
    tm.put(4, new Double(138.00));
    tm.put(2, new Double(919.22));
    tm.put(3, new Double(-119.08));

    List<Map.Entry> valueList = new ArrayList(tm.entrySet());
    Collections.sort(valueList, new MyComparator());

    Iterator<Map.Entry> iterator = valueList.iterator();
    while (iterator.hasNext()) {
      Map.Entry entry = iterator.next();
      System.out.println("Value: " + entry.getValue());
    }
  }
}

The code above generates the following result.