- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
阅读了一些关于 Java 8 的内容,我找到了 this博客文章解释了一些关于流和它们的减少,以及什么时候可以短路减少。底部写着:
Note in the case of
findFirst
orfindAny
we only need the first value which matches the predicate (althoughfindAny
is not guaranteed to return the first). However if the stream has no ordering then we’d expectfindFirst
to behave likefindAny
. The operationsallMatch
,noneMatch
andanyMatch
may not short-circuit the stream at all since it may take evaluating all the values to determine whether the operator istrue
orfalse
. Thus an infinite stream using these may not terminate.
我知道 findFirst
或 findAny
可能会使归约短路,因为一旦找到元素,就不需要进一步处理。
但是为什么这对于 allMatch
、noneMatch
和 anyMatch
是不可能的?对于allMatch
,如果发现一个不匹配谓词,就可以停止处理。没有人也一样。 anyMatch
对我来说尤其没有意义,因为它几乎等同于 findAny
(返回的内容除外)?
对于findFirst/Any
来说,也可以说这三个可能不会短路,因为它可能需要评估所有的值。
我是否遗漏了一些根本的区别?我真的不明白发生了什么吗?
最佳答案
有一个微妙的区别,因为 anyMatch
系列使用谓词,而 findAny
系列没有。从技术上讲,findAny()
看起来像 anyMatch(x -> true)
而 anyMatch(pred)
看起来像 filter(pred).findAny ()
。所以这里我们有另一个问题。考虑我们有一个简单的无限流:
Stream<Integer> s = Stream.generate(() -> 1);
因此,对此类流应用 findAny()
确实总是会短路并完成,而应用 anyMatch(pred)
取决于谓词。然而,让我们过滤我们的无限流:
Stream<Integer> s = Stream.generate(() -> 1).filter(x -> x < 0);
生成的流也是无限的吗?这是一个棘手的问题。它实际上不包含任何元素,但要确定这一点(例如,使用 .iterator().hasNext()
),我们必须检查无限数量的底层流元素,因此此操作永远不会完成。我也将这种流称为无限流。然而,使用这样的流 anyMatch
和 findAny
将永远不会完成:
Stream.generate(() -> 1).filter(x -> x < 0).anyMatch(x -> true);
Stream.generate(() -> 1).filter(x -> x < 0).findAny();
所以 findAny()
也不能保证完成,它取决于之前的中间流操作。
总而言之,我认为该博文极具误导性。在我看来,无限流行为在官方 JavaDoc 中得到了更好的解释。 .
关于Java 8流短路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32192846/
考虑 function foo(x) x isa Bar || throw(ArgumentError("x is not a Int64...")) dosomething(x) e
这个问题在这里已经有了答案: AndAlso/OrElse in VBA (8 个回答) 7年前关闭。 VBA 不会短路 VBA 不支持短路 - 显然是因为它只有按位 And/Or/Not 等操作。来
这个问题在这里已经有了答案: Java operator precedence guidelines (6 个答案) 关闭 7 年前。 你好,我有一个关于 java 中的优先级表的问题。它说 &&
a &= foo(); 只是被 C++ 中的 a = a && foo(); 取代还是还有更多内容? 我关心的是 foo() 的计算。如果 a 为假,它会被调用吗? 最佳答案 && 是否有嵌入式短路:
我正在尝试注释以下代码。 当同时定义了 zone 和 zones 时,或者当定义了 file 时(但不是同时定义了两者),该函数将起作用: def get_file(zone: str, zones:
给定远程beam.smp服务的已知TCP端口和名称,以及已知的cookie,是否可以短路Erlang分发协议(protocol)的Erlang端口映射器守护进程握手阶段并直接建立Erlang shel
我正在尝试注释以下代码。 当同时定义了 zone 和 zones 时,或者当定义了 file 时(但不是同时定义了两者),该函数将起作用: def get_file(zone: str, zones:
我需要从第二个表中查询数据,但前提是满足主表中的一组罕见条件: SELECT ..., IF(a AND b AND c AND (SELECT 1 FROM tableb ...)) FROM ta
如标题所述,我想将 onchange 短路(在选择内部)。我有以下代码,但我只想发送一个硬编码值而不显示选择框。 "> [编辑] 对不起,我不清楚。我什至不想提供
我是 C++ 的新手,很好奇编译器如何处理 bool 值的惰性求值。例如, if(A == 1 || B == 2){...} 如果 A 确实等于 1,那么 B==2 部分是否曾经计算过? 最佳答案
我在尝试让这个 linq 语句起作用时遇到了一些麻烦。我正在使用 linq 查询设置搜索。我想要做的是,如果搜索为空或为空,让它忽略过滤的那一部分。所以我设置了很多 where 子句来缩短 where
这个问题在这里已经有了答案: How to short-circuit a reduce() operation on a Stream? (4 个答案) 关闭 4 年前。 在做项目的时候写了这行,
首先,我不是 Bash 专家。几个月前我发现,如果我同时使用 && 和 || 短路运算符和花括号,那么如果第一个语句以真实值退出,如果 true block 中的最后一条语句退出非零,那么 fail
我有一个使用逻辑 OR 运算符的 NSPredicate,如下所示: NSPredicate(format: "activeFrom == NULL OR %@ >= activeFrom", som
我是 C++ 的新手,很好奇编译器如何处理 bool 值的惰性求值。例如, if(A == 1 || B == 2){...} 如果 A 确实等于 1,那么 B==2 部分是否曾经计算过? 最佳答案
我需要从第二个表中查询数据,但前提是满足主表中的一组罕见条件: SELECT ..., IF(a AND b AND c AND (SELECT 1 FROM tableb ...)) FROM ta
我遇到了这段代码: 我想我们可以归纳为: 然后我找到了这个 article on short circuits和另一个 more focused one一旦我知道他们叫什么。但是,我还是不明白。
据我了解和阅读,您可以在 if 语句(&& 或 ||)中使用短路,以使第二个条件不触发。如果您希望两个条件都触发,您可以使用单个操作数(& 或 |)。 假设我有如下内联 if 语句: var test
在玩弄编译时字符串(char 的可变参数列表)操作时,我需要实现一种方法来检查编译时字符串是否包含另一个(较小的)编译时字符串。 这是我的第一次尝试: template struct Contains
如果我有以下... a OrElse b ... 并且 a 是 真那么显然 b 永远不会被评估。但是如果我添加一个 Or ,然后呢? a OrElse b Or c c 是否/应该得到评估?如果我放入
我是一名优秀的程序员,十分优秀!