每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/