Skip to content

Instantly share code, notes, and snippets.

@seenimohamed
Created January 24, 2018 13:56
Show Gist options
  • Save seenimohamed/5fcdb494c6c400c110efa7780f6f3ebe to your computer and use it in GitHub Desktop.
Save seenimohamed/5fcdb494c6c400c110efa7780f6f3ebe to your computer and use it in GitHub Desktop.
get the highest value from map in java8
//to get key for single max value from map
Integer max=mapGroup.entrySet().stream().max(Map.Entry.comparingByValue()).get().getKey();
//to get keys for for getting all equivalent maximums
private List<Integer> testStreamMap(Map<Integer, Long> mapGroup) {
if(mapGroup.isEmpty())
return Collections.emptyList();
long max = mapGroup.values().stream().max(Comparator.naturalOrder()).get();
return mapGroup.entrySet().stream()
.filter(e -> e.getValue() == max)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment