Parallel Stream non-concurrent unordered collector

前端 未结 2 725
孤城傲影
孤城傲影 2021-01-05 12:59

Suppose I have this custom collector :

  public class CustomToListCollector implements Collector, List> {

     @Ove         


        
2条回答
  •  既然无缘
    2021-01-05 13:40

    This is not an actual answer per-se, but if I add more code and comments, it will get too many I guess.

    Here is another interesting thing, actually it made me realize I was wrong in comments.

    A spliterator flags need to be merged with all the terminal operation flags and intermediate ones.

    Our spliterator's flags are (as reported by StreamOpFlags) : 95; this can be debugged from AbstractSpliterator#sourceSpliterator(int terminalFlags).

    That is why the line below reports true:

     System.out.println(StreamOpFlag.ORDERED.isKnown(95)); // true
    

    At the same time our terminal collector's characteristics are 32:

    System.out.println(StreamOpFlag.ORDERED.isKnown(32)); // false
    

    The result:

    int result = StreamOpFlag.combineOpFlags(32, 95); // 111
    System.out.println(StreamOpFlag.ORDERED.isKnown(result)); // false
    

    If you think about this, it makes complete sense. List has order, my custom collector does not => order is not preserved.

    Bottom-line: that UNORDERED flag is preserved in the resulting Stream, but internally nothing is done with it. They could probably, but they choose not to.

提交回复
热议问题