是的,正如
Stream.findAny()文件所述:
This is a short-circuiting terminal operation.
这是一种常见的误解,即流中的对象被“推”到消耗操作上.它实际上是另一种方式 – 消耗操作拉动每个元素.
对于顺序流,只有在查找匹配值时才会调用许多谓词.
并行流可以执行更多谓词,但也会在找到元素后立即停止执行.
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 found = Arrays.stream(arr).filter(t -> t.getI() == 3).findAny();
System.out.println("Found: "+found.get().getI());
}
}
将打印:
getI: 1
getI: 2
getI: 3
Found: 3