Created
January 24, 2018 13:56
-
-
Save seenimohamed/5fcdb494c6c400c110efa7780f6f3ebe to your computer and use it in GitHub Desktop.
get the highest value from map in java8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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