Last active
July 6, 2018 12:18
-
-
Save nejckorasa/3100ec50708d8efef11692e5e351fc4e to your computer and use it in GitHub Desktop.
Find most frequent item in collection
This file contains hidden or 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
| private <V> V findMostFrequentItem(final Collection<V> items) | |
| { | |
| return items.stream() | |
| .filter(Objects::nonNull) | |
| .collect(Collectors.groupingBy(Functions.identity(), Collectors.counting())).entrySet().stream() | |
| .max(Comparator.comparing(Entry::getValue)) | |
| .map(Entry::getKey) | |
| .orElse(null); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment