fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Map<String,Integer> freqMap = new HashMap<String,Integer>();
  13. freqMap.put("quick", 11);
  14. freqMap.put("brown", 3);
  15. freqMap.put("fox", 6);
  16. freqMap.put("jumps", 8);
  17. freqMap.put("over", 1);
  18. freqMap.put("the", 20);
  19. freqMap.put("lazy", 4);
  20. freqMap.put("dog", 6);
  21. List<Map.Entry<String,Integer>> entries = new ArrayList<Map.Entry<String,Integer>>(
  22. freqMap.entrySet()
  23. );
  24. entries
  25. , new Comparator<Map.Entry<String,Integer>>() {
  26. public int compare(Map.Entry<String,Integer> a, Map.Entry<String,Integer> b) {
  27. return Integer.compare(b.getValue(), a.getValue());
  28. }
  29. }
  30. );
  31. for (Map.Entry<String,Integer> e : entries) {
  32. System.out.println(e.getKey()+":"+e.getValue());
  33. }
  34. }
  35. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
the:20
quick:11
jumps:8
fox:6
dog:6
lazy:4
brown:3
over:1