gpt4 book ai didi

java - 为什么 allMatch 函数在 java 中的空列表上返回 true

转载 作者:行者123 更新时间:2023-12-02 01:37:46 26 4
gpt4 key购买 nike

Java SE 8 doc , allMatch(Predicate<? super T> predicate)

"... evaluates the universal quantification of the predicate over the elements of the stream (for all x P(x)). If the stream is empty, the quantification is said to be vacuously satisfied and is always true (regardless of P(x))."

将空列表视为 allMatch true 似乎并不直观。什么是“普遍量化”和“空洞满足”?

最佳答案

假设我有一个馅饼列表。

我想检查是否所有的馅饼都是山核桃。

所以我看第一个馅饼,它是樱桃,所以我说这是假的。

只有当我能证明我的派不是山核桃时,我才会说这是假的。

所以当我没有馅饼时,没有一个馅饼不是山核桃。

这是一个愚蠢的例子,但这个警告已经让我好几次了

关于java - 为什么 allMatch 函数在 java 中的空列表上返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54975660/

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