是否有帮助程序类在任何标准库中对布尔集合实现逻辑运算?

rip*_*234 4 java collections boolean guava

我编造了这个小帮手类,想知道是否有任何地方我可以从中偷取它而不是重新实现轮子:

public class Booleans3 {
    private Booleans3(){}

    public static boolean and(Iterable<Boolean> booleans) {
        boolean result = true;
        for (Boolean boolRef : booleans) {
            boolean bool = boolRef;
            result &= bool;
        }
        return result;
    }

    public static boolean or(Iterable<Boolean> booleans) {
        boolean result = false;
        for (Boolean boolRef : booleans) {
            boolean bool = boolRef;
            result |= bool;
        }
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

我查看了com.google.common.primitives.Booleans,它似乎没有包含我需要的内容.

Eng*_*uad 12

这个怎么样:

public static boolean and(Collection<Boolean> booleans)
{
    return !booleans.contains(Boolean.FALSE);
}

public static boolean or(Collection<Boolean> booleans)
{
    return booleans.contains(Boolean.TRUE);
}
Run Code Online (Sandbox Code Playgroud)

  • +1简单有效.很好的答案! (4认同)