You can get one key through
Integer max=mapGroup.entrySet().stream().max(Map.Entry.comparingByValue()).get().getKey();
but unfortunately there is no built-in function to get all equivalent maxima.
The simplest, most straightforward solution is to first find the maximum value and after that get all the key mappings:
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()); }
Solutions for obtaining all maximum flow values ββin a single pass are discussed in " How to get max () to return ALL maximum values ββin a Java thread? " You will see that single-pass solutions are much more complicated and not worth the effort if your entry is a regular Map (for example, HashMap ), which can be repeated many times several times cheaper.
source share