About collection (supplier, battery, totalizer)

I do not understand the usefulness of the third parameter of the following method:

<R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner) 

from javaDoc :

This gives a result equivalent to:

  R result = supplier.get(); for (T element : this stream) accumulator.accept(result, element); return result; 

as you can see the combiner parameter combiner not used. For example, the following will accumulate rows in an ArrayList:

  List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll); 

but I expected this:

 List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add ); 
+5
source share
1 answer

combiner used when your Stream is parallel, since in this case several streams collect Stream elements into sub-lists of the final ArrayList output, and these sub-lists must be in order to get the final ArrayList .

+10
source

All Articles