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

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