gpt4 book ai didi

java - 为什么只有在使用 count() 函数时才执行 map() 函数内的表达式?

转载 作者:行者123 更新时间:2023-11-30 01:56:42 25 4
gpt4 key购买 nike

当我使用count()时函数“内部测试”消息按预期调用了三次,但是当我删除 count( 时) 函数调用test()函数没有被调用。来自 count()函数文档我知道它返回给定流中的元素计数。

public class Start {
public static int test(int input) {
System.out.println("inside processRecord");
return input;
}
public static void main(String[] args) throws InterruptedException {
List<Integer> data = Arrays.asList(1,2,3);
data.parallelStream().map(Start::test).count();
}
}

最佳答案

因为 count 是一种终端操作,并且仅当存在终端操作时才会调用/执行流;据说他们很懒...

请注意,在 java-9 及更高版本中,您的示例不会以任何方式从 map 打印这些语句,因为您关心的是有多少map 有点没用...

关于java - 为什么只有在使用 count() 函数时才执行 map() 函数内的表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54214497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com