如何在 Java 中编写以下 python 行?
a = [True, False]
any (a)
all (a)
inb4“你试过什么?”
大锤式的方法是编写我自己的 all
和 any
方法(显然还有一个 class to host them ):
public boolean any (boolean [] items)
{
for (boolean item: items)
if (item) return true;
return false;
}
//other way round for all
但我不打算重新发明轮子,必须有一种巧妙的方法来做到这一点......
最佳答案
any()
与 Collection#contains()
相同,是标准库的一部分,实际上是所有 Collection
实现的实例方法。
然而,没有内置的all()
。除了你的“大锤”方法之外,你最接近的是 Google Guava's Iterables#all()
.
关于Java相当于python all and any,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18940246/