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(Supplier<R> supplier, BiConsumer<R,T> accumulator, BiConsumer<R,R> combiner) operation of the Stream<T> interface that produces the Map object. Create Map<String, Person> with a person's name as the key: 
Map<String, Person> map = Stream.of(new Person(30, "John"),                                     new Person(20, "Jill"))        .collect(HashMap::new,                (m,p) -> m.put(p.getName(), p),                Map::putAll        );System.out.println(map); //prints: {John=Person{name:John,age:30},                          //         Jill=Person{name:Jill,age:20}}

Or, to avoid redundant data in the resulting Map, we can use age field as the Map value:

Map<String,  ...

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.