You will learn and practice how to use the collect() terminal operation to repackage stream elements to a target collection structure.
Completing streams by producing collections
Getting ready
There are two overloaded versions of the collect() terminal operation that allow us to create a collection of the stream elements:
- R collect(Supplier<R> supplier, BiConsumer<R,T> accumulator, BiConsumer<R,R> combiner): Produces the R result using the passed-in functions applied to the stream elements of the T type. The provided supplier and accumulator work together as follows:
R result = supplier.get();
for (T element : this stream) {
accumulator.accept(result...