考虑这段代码
Object found = collection.stream()
.filter( s -> myPredicate1(s))
.filter( s -> myPredicate2(s))
.findAny()
它会处理整个流并调用两者吗myPredicate1
and myPredicate2
对于集合的所有元素?或者是否会根据实际查找值的需要调用尽可能多的谓词?
是的,确实如此,因为Stream.findAny() https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#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());
}
}
将打印:
getI: 1
getI: 2
getI: 3
Found: 3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)