Код: Выделить всё
/**
Parameters:
supplier - a function that creates a new result container. For a parallel execution, this function may be called multiple times and must return a fresh value each time.
accumulator - an associative, non-interfering, stateless function for incorporating an additional element into a result
combiner - an associative, non-interfering, stateless function for combining two values, which must be compatible with the accumulator function
Returns:
the result of the reduction
*/
R collect(Supplier supplier,
BiConsumer accumulator,
BiConsumer combiner)
< /code>
Я видел множество примеров, в которых используются комбинации Thread-Unsafe, как в блогах онлайн, так и в книгах. Например, в этом сообщении приведен пример, в котором используется в функции Combiner-unsafe stringBuilder Код: Выделить всё
List vowels = List.of("a", "e", "i", "o", "u");
// sequential stream - nothing to combine
StringBuilder result = vowels.stream().collect(StringBuilder::new, (x, y) -> x.append(y),
(a, b) -> a.append(",").append(b));
System.out.println(result.toString());
// parallel stream - combiner is combining partial results
StringBuilder result1 = vowels.parallelStream().collect(StringBuilder::new, (x, y) -> x.append(y),
(a, b) -> a.append(",").append(b));
System.out.println(result1.toString());
< /code>
Я знаю, что Biconsumer Accumulator Подробнее здесь: https://stackoverflow.com/questions/796 ... -in-java-8