如何确定集合中的所有元素是否具有与流IPA相同的值

发布于 2025-01-20 20:30:46 字数 1572 浏览 0 评论 0原文

我正在尝试搜索集合并根据集合中的每个项目是否都有一个返回truefalse匹配 boolean 属性。

我做了一个简单的示例,其中“falseList”返回 false

所以我的问题是,是否有一个流调用可以干净地检查集合中匹配的 boolean 值,这样“falseList”将导致 true 因为所有属性都匹配?

我知道我可以获取集合中的第一项,然后使用 2 行中的 allMatch() 将所有值与第一项进行比较,但是我正在寻找出于好奇,单线。

class BooleanClass {
    boolean val;

    public BooleanClass(boolean val) {
        this.val = val; 
    }

    public boolean getVal() {
        return this.val;
    }
}

public class AllMatch {
    
    public static void main(String[] args) {
        BooleanClass a = new BooleanClass(true);
        BooleanClass b = new BooleanClass(true);
        BooleanClass c = new BooleanClass(true);

        ArrayList<BooleanClass> trueList = new ArrayList<BooleanClass>();
        trueList.add(a);
        trueList.add(b);
        trueList.add(c);

        System.out.println(trueList.stream().allMatch(l -> l.getVal())); // true

        BooleanClass d = new BooleanClass(false);
        BooleanClass e = new BooleanClass(false);
        BooleanClass f = new BooleanClass(false);

        ArrayList<BooleanClass> falseList = new ArrayList<BooleanClass>();
        falseList.add(d);
        falseList.add(e);
        falseList.add(f);

        System.out.println(falseList.stream().allMatch(l -> l.getVal())); // false
    }
}

当前输出

true
false

期望输出

true
true

I am trying to search a collection and return true or false based on if every item in the collection has a matching boolean property.

I made a simple example where the 'falseList' returns false.

So my question, is there a stream call to cleanly check for matching boolean values in a collection, such that the 'falseList' will result in a true since all properties match?

I'm aware that I can get the first item in the collection and then compare all values to the first item with allMatch() in 2 lines, however I am looking for a one liner out of curiosity.

class BooleanClass {
    boolean val;

    public BooleanClass(boolean val) {
        this.val = val; 
    }

    public boolean getVal() {
        return this.val;
    }
}

public class AllMatch {
    
    public static void main(String[] args) {
        BooleanClass a = new BooleanClass(true);
        BooleanClass b = new BooleanClass(true);
        BooleanClass c = new BooleanClass(true);

        ArrayList<BooleanClass> trueList = new ArrayList<BooleanClass>();
        trueList.add(a);
        trueList.add(b);
        trueList.add(c);

        System.out.println(trueList.stream().allMatch(l -> l.getVal())); // true

        BooleanClass d = new BooleanClass(false);
        BooleanClass e = new BooleanClass(false);
        BooleanClass f = new BooleanClass(false);

        ArrayList<BooleanClass> falseList = new ArrayList<BooleanClass>();
        falseList.add(d);
        falseList.add(e);
        falseList.add(f);

        System.out.println(falseList.stream().allMatch(l -> l.getVal())); // false
    }
}

Current output:

true
false

Desired output:

true
true

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

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

发布评论

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

评论(2

墨离汐 2025-01-27 20:30:47

根据集合中的每个项目是否具有匹配的布尔属性返回 true 或 false

因此,您需要检查 list 中的所有元素是否具有相同的值(全部 true或全部false)。您希望在单个流语句中实现这一点。

您可以使用 anyMatch() 来完成此操作。使用此操作进行检查相对于 allMatch() 的优势在于 anyMatch() 是一个短路手术。即当它遇到与给定谓词不匹配的元素时,它会终止流管道的执行并返回结果。

它可能看起来像这样:

public static boolean isNonUniform(Collection<Boolean> list) {
    return list.stream().anyMatch(el -> el.getVal() != falseList.get(0).getVal());
}

方法 isNonUniform() 顾名思义,以及如何从传递到 anyMatch()谓词 中看到,检查给定集合是否至少包含一个与其他集合不同的元素。

注意,访问列表的第一个元素安全,因为如果列表为空,则谓词将不会被评估。

另请注意,当流中没有元素时,anyMatch() 将返回 false。即空列表被认为是统一的(不是非统一的)。如果您想在列表为空时获取 true,请将此条件附加在流前面:falseList.isEmpty() ||。从技术上讲,它仍然是一句俏话。

因此,为了确定列表是否统一,应该这样使用:

System.out.println(!isNonUniform(falseList));

旁注:我个人认为有 notnon 在方法名称中(这并不完美)从干净编码的角度来看是较小的邪恶,然后将逻辑非 ! 放在流前面,这使得它变得更难阅读。

return true or false based on if every item in the collection has a matching boolean property

So you need to check whether all elements in a list have the same value (either all true or all false). And you want to achieve that in a single stream statement.

You can do it with anyMatch(). The upper hand of checking with this operation over allMatch() is that anyMatch() is a short-circuit operation. I.e. when it encounters the element that doesn't match the given predicate, it terminates the execution of the stream pipe-line and return the result.

That how it might look like:

public static boolean isNonUniform(Collection<Boolean> list) {
    return list.stream().anyMatch(el -> el.getVal() != falseList.get(0).getVal());
}

Method isNonUniform() as its name suggests and how you can see from the predicate passed into anyMatch(), checks whether the given collection contains at least one element that is different from the others.

Note, that accessing the first element of the list is safe because if the list is empty, the predicate will not be evaluated.

Also note that anyMatch() will return false when there are no elements in the stream. I.e. an empty list is considered to be uniform (not nonUniform). In case if you want to obtain true when the list is empty, then append this condition in front of the stream: falseList.isEmpty() ||. Technically, it still will be a one-liner.

So in order to determine if the list is uniform, it should be used this way:

System.out.println(!isNonUniform(falseList));

Side-note: I personally think that having not or non in the method name (which is not perfect) from the clean-coding perspective is smaller evil, then placing logical not ! in front of the stream, which makes it harder to read.

爱冒险 2025-01-27 20:30:47

首先,您的问题提到了 allMatch,但是,您在代码中使用了 anyMatch。无论哪种方式,对于计算结果为 true 的内容,lambda 计算结果应为 true。在你的情况下,第一个表达式 l ->; l.getVal() 的计算结果始终为 true。对于第二个表达式 l -> l.getVal() 的计算结果始终为 false,因此有效结果将为 false。要使其为 true,您必须将 lambda 更改为 l -> l.getVal() == false。不确定这是否是您想要实现的目标。

Firstly, your question mentions allMatch, however, you are using anyMatch in your code. Either way, for something to evaluate to true, the lambda should evaluate to true. In your case, the first expression l -> l.getVal() always evaluates to true. For second expression l -> l.getVal() always evaluates to false, hence the effective result will be false. To make it true, you have to change the lambda to l -> l.getVal() == false. Not sure if that's what you want to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文