简体   繁体   中英

Confusion about Characteristics.UNORDERED in Java 8 in action book

The author of java 8 in action writes this class:

class ToListCollector<T> implements Collector<T, List<T>, List<T>> {

    @Override
    public Supplier<List<T>> supplier() {
        return ArrayList::new;
    }

    @Override
    public BiConsumer<List<T>, T> accumulator() {
        return List::add;
    }

    @Override
    public BinaryOperator<List<T>> combiner() {
        return (l1, l2) -> {
            l1.addAll(l2);
            return l1;
        };
    }

    @Override
    public Function<List<T>, List<T>> finisher() {
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT));
    }
}

Then he talks about what different values in Characteristic enum mean. And then he explains why this collector he wrote is IDENTITY_FINISH and CONCURRENT and not UNORDERED, saying:

The ToListCollector developed so far is IDENTITY_FINISH, because the List used to accumulate the elements in the stream is already the expected final result and doesn't need any further transformation, but it isn't UNORDERED because if you apply it to an ordered stream you want this ordering to be preserved in the resulting List. Finally, it's CONCURRENT, but following what we just said, the stream will be processed in parallel only if its underlying data source is unordered.

Why will the stream be processed in parallel only if the underlying source is unordered? I think it will be still processed in parallel but combiner() will have to preserve order. Is it an error in the book?

I think Brian Goetz quite clearly talks about parallel processing of ordered streams in this post in the last parahraph.

The pages in the book are 192 - 193.

That is simply wrong. Even adding CONCURRENT characteristic here is wrong, as you would need a thread safe data structure in the Supplier .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM