O'Reilly logo

Java 11 Cookbook by Mohamed Sanaulla, Nick Samoylov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

We will walk you through the sequence of practical steps that demonstrate how to use the preceding methods and classes:

  1. Write an example of the usage of the R collect(Collector<T, A, R> collector) operation of the Stream<T> interface with the collector created by the Collector<T, ?, Map<K,List<T>>> groupingBy(Function<T,K> classifier) method:
Map<String, List<Person>> map = getStreamPerson()        .collect(Collectors.groupingBy(Person::getName));System.out.println(map);                 //prints: {John=[Person{name:John,age:30},                //               Person{name:John,age:20}],                //         Jill=[Person{name:Jill,age:20}]}

This is the simplest version of the Collector object. You just define what is going to be the key of the resulting map, and the collector will add all ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required