Java stream order of processing

后端 未结 2 490
不知归路
不知归路 2021-01-13 08:37

I have the following:

LinkedList ints = new LinkedList();
//fill it with some ints
Stream stream = ints.stream();
//process the         


        
相关标签:
2条回答
  • 2021-01-13 08:58

    Depends on the processing you are applying to the stream... especially using parallel()

    import java.util.LinkedList;
    import java.util.function.Consumer;
    import java.util.stream.Stream;
    
    public class Streaming {
    
        public static void main(String[] args) {
            LinkedList<Integer> ints = new LinkedList();
            for(int i = 0 ; i < 100; i++) {
                ints.add(i);
            }
            Stream<Integer> stream = ints.stream();
            // will not be ordered
            stream.parallel().forEach(new Consumer<Integer>() {
    
                @Override
                public void accept(Integer t) {
                    System.out.println(t);
                }
    
            });
    
            stream = ints.stream();
            // will be ordered
            stream.parallel().forEachOrdered(new Consumer<Integer>() {
    
                @Override
                public void accept(Integer t) {
                    System.out.println(t);
                }
    
            });
        }
    }
    
    0 讨论(0)
  • 2021-01-13 08:59

    From the documentation:

    Streams may or may not have a defined encounter order. Whether or not a stream has an encounter order depends on the source and the intermediate operations. Certain stream sources (such as List or arrays) are intrinsically ordered, whereas others (such as HashSet) are not. Some intermediate operations, such as sorted(), may impose an encounter order on an otherwise unordered stream, and others may render an ordered stream unordered, such as BaseStream.unordered(). Further, some terminal operations may ignore encounter order, such as forEach().

    If a stream is ordered, most operations are constrained to operate on the elements in their encounter order; if the source of a stream is a List containing [1, 2, 3], then the result of executing map(x -> x*2) must be [2, 4, 6]. However, if the source has no defined encounter order, then any permutation of the values [2, 4, 6] would be a valid result.

    For sequential streams, the presence or absence of an encounter order does not affect performance, only determinism. If a stream is ordered, repeated execution of identical stream pipelines on an identical source will produce an identical result; if it is not ordered, repeated execution might produce different results.

    For parallel streams, relaxing the ordering constraint can sometimes enable more efficient execution...

    Also, as you mentioned that processing order matters for you see here:

    Stream pipeline results may be nondeterministic or incorrect if the behavioral parameters to the stream operations are stateful...

    The best approach is to avoid stateful behavioral parameters to stream operations entirely; there is usually a way to restructure the stream pipeline to avoid statefulness.

    See also the answer to this question: How to ensure order of processing in java8 streams?

    In short, it looks like you can preserve the order, if you use sequential streams (streams that are executed in one thread) and if you are careful with operations like forEach(). However it probably is not a good idea.

    0 讨论(0)
提交回复
热议问题