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(Supplier<R> supplier, BiConsumer<R,T> accumulator, BiConsumer<R,R> combiner) operation of the Stream<T> interface that produces the List<T> object:
List<Person> list = Stream.of(new Person(30, "John"), new Person(20, "Jill")) .collect(ArrayList::new, List::add, //same as: (a,p)-> a.add(p), List::addAll //same as: (r, r1)-> r.addAll(r1) ); System.out.println(list); //prints: [Person{name:John,age:30}, Person{name:Jill,age:20}]
In the preceding example, the comments to the accumulator and the combiner demonstrate how these functions can be presented as lambda ...