We will walk you through the sequence of practical steps that demonstrate how to use the preceding methods and classes:
- 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 ...