fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Set;
  10. import java.util.Map.Entry;
  11.  
  12.  
  13. /* Name of the class has to be "Main" only if the class is public. */
  14. class Ideone
  15. {
  16. public static void main (String[] args) throws java.lang.Exception
  17. {
  18. Map<String, Integer> map = new HashMap<String, Integer>();
  19. map.put("java", 20);
  20. map.put("C++", 45);
  21. map.put("Java2Novice", 2);
  22. map.put("Unix", 67);
  23. map.put("MAC", 26);
  24. map.put("Why this kolavari", 93);
  25. Set<Entry<String, Integer>> set = map.entrySet();
  26. List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
  27. Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
  28. {
  29. public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
  30. {
  31. return (o1.getValue()).compareTo( o2.getValue() );
  32. }
  33. } );
  34. for(Map.Entry<String, Integer> entry:list){
  35. System.out.println(entry.getKey()+" ==== "+entry.getValue());
  36. }
  37.  
  38. }
  39. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
Java2Novice ==== 2
java ==== 20
MAC ==== 26
C++ ==== 45
Unix ==== 67
Why this kolavari ==== 93