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 ...

Get Java 11 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.