How to correctly find the stream characteristics in Java-8?

后端 未结 2 673
离开以前
离开以前 2021-02-05 13:35

While doing stream operations, during the intermediate/pipeline operations the streams would be created with different characteristics(e.g: SORTED/SIZED/DISTINCT/ORDERED) - Mast

2条回答
  •  野性不改
    2021-02-05 13:56

    At each stage you can call:

    int c = stream.spliterator().characteristics();
    

    And then test the result against the constants defined in the Spliterator class. For example to see if the stream is ordered:

    boolean isOrdered = (c & Spliterator.ORDERED) == Spliterator.ORDERED;
    

    Alternatively you can use:

    boolean isOrdered = stream.spliterator().hasCharacteristics(Spliterator.ORDERED);
    

提交回复
热议问题