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, String> values = new HashMap<>();
  13.  
  14. values.put("foo", "c");
  15. values.put("bar", "b");
  16. values.put("baz", "a");
  17.  
  18. for (Map.Entry<String, String> entry : values.entrySet()) {
  19. System.out.println(entry);
  20. }
  21. System.out.println("-----------sort-----------");
  22. List<Map.Entry<String, String>> entries = new ArrayList(values.entrySet());
  23. Collections.sort(entries, new Comparator<Map.Entry<String, String>>() {
  24.  
  25. public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
  26. return o1.getValue().compareTo(o2.getValue());
  27. }
  28. });
  29. for (Map.Entry<String, String> entry : entries) {
  30. System.out.println(entry);
  31. }
  32. }
  33. }
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
baz=a
foo=c
bar=b
-----------sort-----------
baz=a
bar=b
foo=c