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. import java.util.stream.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. Map<String,Integer> map = new HashMap<String,Integer>();
  14. map.put("eggs",1500);
  15. map.put("echo",150);
  16. map.put("foo",320);
  17. map.put("smt",50);
  18.  
  19.  
  20. List<Map.Entry<String,Integer>> sortedList = new ArrayList<Map.Entry<String,Integer>>(
  21. map
  22. .entrySet()
  23. .stream()
  24. .sorted((o1, o2) -> (o2.getValue()).compareTo( o1.getValue()))
  25. .collect(Collectors.toList())
  26. );
  27. List<String> word_used = new ArrayList<String>(
  28. sortedList
  29. .stream()
  30. .map(Map.Entry<String,Integer>::getKey)
  31. .collect(Collectors.toList())
  32. );
  33. List<Integer> ints_used = new ArrayList<Integer>(
  34. sortedList
  35. .stream()
  36. .map(Map.Entry<String,Integer>::getValue)
  37. .collect(Collectors.toList())
  38. );
  39.  
  40. for (String s : word_used) {
  41. System.out.println(s);
  42. }
  43. for (Integer i : ints_used) {
  44. System.out.println(i);
  45. }
  46. }
  47. }
Success #stdin #stdout 0.21s 320832KB
stdin
Standard input is empty
stdout
eggs
foo
echo
smt
1500
320
150
50