Java相当于python all and any

标签 java python

如何在 Java 中编写以下 python 行?

a = [True, False]
any (a)
all (a)

inb4“你试过什么?”

大锤式的方法是编写我自己的 allany 方法(显然还有一个 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/

相关文章:

php - Django 为同一模型创建不同的表

python - wxPython 和 windows 7 任务栏

python - Pandas 数据框列表部分字符串匹配python

python - 如何在 Python 中使用 SQLite 3 的 Vacuum 命令

java - iBatis只生成6个参数(全部为null),其他时候生成9个参数

java - 程序可以在我的电脑上运行,但不能在服务器上运行

python - 在 Python 中调用实例变量时遇到问题

java - android OpenCV中的卡通效果(需要从灰色转换为彩色)

java - 发送没有关键值的改造获取请求

java - javaee-api 和 javaee-web-api 有什么区别?