In jdk8,the Stream provide collect
function with supplier
,accumulator
and combiner
<R> R collect(Supplier<R> supplier,BiConsumer<R, ? super T> accumulator,BiConsumer<R, R> combiner);
I see the example in the annotation that transform a String Stream to a String,but I am very confused what the use of combiner in the function signature. I think in the accumulator,the new element has been added to the result continer?
I have tried give the combiner a null value,but I got a null pointer exception.
Stream<String> stringStream = Stream.of("hello", "world", "morning");
String string = stringStream.collect(StringBuilder::new,
StringBuilder::append, StringBuilder::append).toString();
System.out.println(string);
Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations * can be parallelized without requiring additional synchronization.`
– Yassin Hajaj Jul 14 '19 at 08:09