Stream.findAny是短路操作吗?

Dar*_*usz 4 java short-circuiting java-8 java-stream

考虑这段代码

Object found = collection.stream()
    .filter( s -> myPredicate1(s))
    .filter( s -> myPredicate2(s))
    .findAny()
Run Code Online (Sandbox Code Playgroud)

它会处理整个流,并呼吁双方myPredicate1以及myPredicate2为集合中的所有元素?或者,实际找到该值需要调用多少个谓词?

Dar*_*usz 5

是的,正如Stream.findAny()文件所述:

这是一种短路终端操作.

这是一种常见的误解,即流中的对象被"推"到消耗操作上.它实际上是另一种方式 - 消耗操作拉动每个元素.

对于顺序流,只有在查找匹配值时才会调用许多谓词.并行流可以执行更多谓词,但也会在找到元素后立即停止执行.

public class StreamFilterLazyTest {

  static int stI = 0;

  static class T { 

    public T() {
      super();
      this.i = ++stI;
    }

    int i;

    int getI() {
      System.err.println("getI: "+i);
      return i;
    }
  }

  public static void main(String[] args) {
    T[] arr = {new T(), new T(), new T(), new T(), new T(), new T(), new T(), new T(), new T(), new T()};
    Optional<T> found = Arrays.stream(arr).filter(t -> t.getI() == 3).findAny();
    System.out.println("Found: "+found.get().getI());
  }
}
Run Code Online (Sandbox Code Playgroud)

将打印:

getI: 1
getI: 2
getI: 3
Found: 3
Run Code Online (Sandbox Code Playgroud)