Skip to content

Instantly share code, notes, and snippets.

@nejckorasa
Last active July 6, 2018 12:18
Show Gist options
  • Select an option

  • Save nejckorasa/3100ec50708d8efef11692e5e351fc4e to your computer and use it in GitHub Desktop.

Select an option

Save nejckorasa/3100ec50708d8efef11692e5e351fc4e to your computer and use it in GitHub Desktop.
Find most frequent item in collection
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