Skip to content

Instantly share code, notes, and snippets.

@vladholubiev
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save vladholubiev/958c2169e7d6c9a96b64 to your computer and use it in GitHub Desktop.

Select an option

Save vladholubiev/958c2169e7d6c9a96b64 to your computer and use it in GitHub Desktop.
Map sorted by values in a descending order
Map<String, Integer> map = new HashMap<>();
Map<String, Integer> sortedByValues;
map.put("C++", 14);
map.put("Java", 8);
map.put("Python", 3);
sortedByValues = map.entrySet().stream()
.sorted(Collections.reverseOrder(comparing(Entry::getValue)))
.collect(toMap(Entry::getKey, Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
System.out.println(sortedByValues);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment