Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 218
Coder-Man Avatar asked May 31 '18 13:05

Coder-Man


People also ask

What does .collect do in Java?

collect() is one of the Java 8's Stream API's terminal methods. It allows us to perform mutable fold operations (repackaging elements to some data structures and applying some additional logic, concatenating them, etc.) on data elements held in a Stream instance.

What is import Java Util Stream collectors?

Collectors is one of the utility class in JDK which contains a lot of utility functions. It is mostly used with Stream API as a final step. In this article, we will study different methods in the collector class.

What is characteristics method Java?

The characteristics() is a method of Java Interface Spliterator which is used to get a set of characteristics of this Spliterator and its elements.


1 Answers

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

like image 52
Eugene Avatar answered Oct 30 '22 12:10

Eugene