In statistics, this is called a "mode . " Vanilla Java 8 solution looks like this:
itinerary .stream() .flatMap(i -> StreamSupport.stream( Spliterators.spliteratorUnknownSize(i.iterator(), 0) )) .collect(Collectors.groupingBy( s -> s.getDestination().toLowerCase().replace(" ", ""), Collectors.counting() )) .entrySet() .stream() .max(Comparator.comparing(Entry::getValue)) .ifPresent(System.out::println);
jOOλ is a library that supports mode() by stream. The following program:
System.out.println( Seq.seq(itinerary) .flatMap(i -> Seq.seq(i.iterator())) .map(s -> s.getDestination().toLowerCase().replace(" ", "")) .mode() );
(disclaimer: I work for jOOλ in the company)
source share