examples i have seen for finding mode using stream api return a single mode and if there are two elements that occur equally, the first is returned. however, returning an array with both (or more than 2) elements i can find no example of. i am not sure if there is a simple tweak of the following code:
Integer mode = list.stream()
.collect(Collectors.groupingBy(i -> i, () -> new TreeMap<Integer, Long>(), Collectors.counting()))
.entrySet().stream().sorted((a, b) -> {
if (!a.getValue().equals(b.getValue()))
return b.getValue().compareTo(a.getValue());
return a.getKey().compareTo(b.getKey());
}).findFirst().get().getKey();
Instead of Integer mode I am trying to get Integer[] mode
or List<Integer> mode
.