Guest User

Untitled

a guest
Mar 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. HashMap<String, Integer> map = new HashMap<String, Integer>();
  2. map.put("a", 10);
  3. map.put("b", 30);
  4. map.put("c", 50);
  5. map.put("d", 40);
  6. map.put("e", 100);
  7. map.put("f", 60);
  8. map.put("g", 110);
  9. map.put("h", 50);
  10. map.put("i", 90);
  11. map.put("k", 70);
  12. map.put("L", 80);
  13.  
  14. map.entrySet().stream()
  15. .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
  16. .limit(10)
  17. .forEach(System.out::println); // or any other terminal method
  18.  
  19. Map<String,Person> map = new HashMap<>();
  20. map.put("g",new Person(5, "EE", 51, Person.SEX.FEMALE, "A"));
  21. map.put("a",new Person(4, "DD", 25, Person.SEX.MALE, "D"));
  22. map.put("e",new Person(3, "CC", 44, Person.SEX.FEMALE,"B"));
  23.  
  24. Map<String,Person> sortedNewMap = map.entrySet().stream().sorted((e1,e2)->
  25. e1.getValue().getLocation().compareTo(e2.getValue().getLocation()))
  26. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
  27. (e1, e2) -> e1, LinkedHashMap::new));
  28. sortedNewMap.forEach((key,val)->{
  29. System.out.println(key+ " = "+ val.toString());
  30. });
  31.  
  32. List<Map.Entry<String, Integer>> entries = newArrayList(map.entrySet());
  33. Collections.sort(entries, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
  34. List<Map.Entry<String, Integer>> top10 = entries.subList(0, Math.min(entries.size(), 10));
  35.  
  36. Map<String,Person> map = new HashMap<>();
  37. map.put("g",new Person(5, "EE", 51, Person.SEX.FEMALE, "A"));
  38. map.put("a",new Person(4, "DD", 25, Person.SEX.MALE, "D"));
  39. map.put("e",new Person(3, "CC", 44, Person.SEX.FEMALE,"B"));
  40.  
  41. Map<String,Person> sortedNewMap = map.entrySet().stream().sorted((e1,e2)->
  42. Integer.compare(e1.getValue().getAge(), e2.getValue().getAge()))
  43. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
  44. (e1, e2) -> e1, LinkedHashMap::new));
Add Comment
Please, Sign In to add comment