Suppose, however, that the result container used in this reduction was a concurrently modifiable collection -- such as a ConcurrentHashMap. In that case, the
Your Collector
does not know that you use a concurrent collection provided by Supplier
, just add the characteristic and see that it is executed the way you want to; for example:
String s = Stream.of(1, 2, 3, 4).parallel()
.unordered()
.collect(
Collector.of(
() -> new ConcurrentLinkedQueue<>(),
(c, e) -> c.add(e.toString()),
(c1, c2) -> {
c1.addAll(c2);
return c1;
},
Characteristics.CONCURRENT))