是否有任何标准库中的辅助类可以对布尔集合实现逻辑操作?

发布于 2024-12-25 19:23:43 字数 809 浏览 1 评论 0原文

我编写了这个小助手类,并想知道是否可以从任何地方窃取它,而不是重新实现轮子:

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;
    }
}

我查看了 com.google.common.primitives.Booleans,它似乎不包含我需要什么。

I concocted this little helper class, and wanted to know if there's anywhere I can steal it from instead of re-implementing the wheel:

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;
    }
}

I looked at com.google.common.primitives.Booleans, and it doesn't seem to contain what I need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

池予 2025-01-01 19:23:43

这个怎么样:

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

public static boolean or(Collection<Boolean> booleans)
{
    return booleans.contains(Boolean.TRUE);
}

How about this:

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

public static boolean or(Collection<Boolean> booleans)
{
    return booleans.contains(Boolean.TRUE);
}
浅唱ヾ落雨殇 2025-01-01 19:23:43

虽然 @eng-fouad 的答案已经足够好了,但我仍然建议使用另一个 Iterables.all()Iterables.any()equalTo 谓词:

import java.util.Arrays;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

import static com.google.common.collect.Iterables.all;
import static com.google.common.collect.Iterables.any;
import static com.google.common.base.Predicates.equalTo;

...

Iterable<Boolean> booleans = Arrays.asList(TRUE, TRUE, FALSE);

System.out.println(all(booleans, equalTo(TRUE)));
System.out.println(any(booleans, equalTo(TRUE)));

将打印

false
true

我看到的优点:

  • 使用 Guava 库
  • 不需要编写自己的类使用方法
  • 更好的可读性(恕我直言)

While @eng-fouad answer is good enough I still suggest another one which utilizes Iterables.all() and Iterables.any() with equalTo predicate:

import java.util.Arrays;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

import static com.google.common.collect.Iterables.all;
import static com.google.common.collect.Iterables.any;
import static com.google.common.base.Predicates.equalTo;

...

Iterable<Boolean> booleans = Arrays.asList(TRUE, TRUE, FALSE);

System.out.println(all(booleans, equalTo(TRUE)));
System.out.println(any(booleans, equalTo(TRUE)));

will print

false
true

As pluses I see:

  • Uses Guava library
  • No need to write own class with methods
  • Better readability (IMHO)
猫性小仙女 2025-01-01 19:23:43

我不相信 Java 标准库的任何部分能够提供这种功能。

在某些语言中,它们以称为 anyOf(对于 or)或 allOf(对于 and)的函数的形式提供。您可能会幸运地搜索实现这些功能的 Java 库。

请注意,您这里的代码可以进行相当多的优化。请注意,如果您正在计算许多布尔值的 AND,一旦发现 false 值,您就可以停下来并说答案是 false。同样,如果您正在计算布尔值的 OR,则可以在找到 true 值后立即停止,因为答案将为 true。这本质上是短路评估对对象列表的概括,并且是行为一些版本的 andor 在像Scheme这样的语言中。这给出了以下内容:

public class ModifiedBooleans3 {
    private ModifiedBooleans3(){}

    public static boolean and(Iterable<Boolean> booleans) {
        for (Boolean boolRef : booleans)
            if (!boolRef)
                return false;
        return true;
    }

    public static boolean or(Iterable<Boolean> booleans) {
        for (Boolean boolRef : booleans)
            if (boolRef)
                return true;
        return false;
    }
}

希望这有帮助!

I don't believe that there is any part of the Java Standard Libraries that provides exactly this functionality.

In some languages, these are provided as functions called anyOf (for or) or allOf (for and). You may have some luck searching for Java libraries implementing those functions.

A note-, the code you have here can be optimized quite a bit. Note that if you're computing the AND of many boolean values, as soon as you find a false value you can stop and say that the answer is false. Similarly, if you're computing the OR of boolean values, you can stop as soon as you find a true value, since the answer will be true. This is essentially a generalization of short-circuit evaluation to lists of objects, and is the behavior of some versions of and and or in languages like Scheme. This gives the following:

public class ModifiedBooleans3 {
    private ModifiedBooleans3(){}

    public static boolean and(Iterable<Boolean> booleans) {
        for (Boolean boolRef : booleans)
            if (!boolRef)
                return false;
        return true;
    }

    public static boolean or(Iterable<Boolean> booleans) {
        for (Boolean boolRef : booleans)
            if (boolRef)
                return true;
        return false;
    }
}

Hope this helps!

情徒 2025-01-01 19:23:43

对 @Eng.Fouad 答案的改进:

如果 boolean 集合为 null,以下代码返回 false >empty,否则对元素进行逻辑运算:

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

public static boolean or(Collection<Boolean> booleans) {
    return booleans != null && !booleans.isEmpty() && booleans.contains(Boolean.TRUE);
}

An Improvement on @Eng.Fouad answer:

The following code return false if boolean collection is null or empty, otherwise do logical operation on elements:

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

public static boolean or(Collection<Boolean> booleans) {
    return booleans != null && !booleans.isEmpty() && booleans.contains(Boolean.TRUE);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文