【发布时间】:2020-06-17 21:44:24
【问题描述】:
public class Test {
public static void main(String[] args) {
long filtered = Stream.of("test1", "test2", "test3")
.filter(getPredicate())
.count();
System.out.println(filtered);
}
private static Predicate<String> getPredicate() {
System.out.println("print this");//<-- this line was printed only once
return item -> item.contains("test");
}
}
我预计上面的代码会打印 3 次 打印这个。但它只打印了一次,谁能解释一下?
【问题讨论】:
-
你的代码调用
getPredicate()多少次? -
它被调用过一次
标签: java