fork download
  1.  
  2. import java.util.*;
  3.  
  4. class KeyComparator implements Comparator
  5. {
  6. public int compare(Object a, Object b)
  7. {
  8. if ((char) a > (char) b)
  9. return 1;
  10. else
  11. return -1;
  12. }
  13. }
  14.  
  15. class Tester
  16. {
  17. public static void main(String[] args)
  18. {
  19. Map map = new HashMap();
  20. map.put('B', 3);
  21. map.put('A', 1);
  22. map.put('C', 2);
  23.  
  24. Map sortedmap = new TreeMap(new KeyComparator());
  25. sortedmap.putAll(map);
  26.  
  27. System.out.print(sortedmap);
  28. }
  29. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
{A=1, B=3, C=2}