Java - 如何创建谓词作为 Arrays.asList() 参数?

发布于 2025-01-11 13:51:21 字数 783 浏览 0 评论 0原文

我有一些谓词想要放入列表中,这样我就可以在列表中调用stream().noneMatch()。我通过创建命名谓词成功地做到了这一点,但如何在 Arrays.asList() 的参数列表中创建它们?

这是我想要转换的工作代码:

ArrayList<Predicate<MyClass>> checks = new ArrayList<>();
Predicate<MyClass> isCond1 = myClassHelper::isCond1;
Predicate<MyClass> isCond2 = myClassHelper::isCond2;
checks.add(isCond1);
checks.add(isCond2);

我希望转换的结果看起来像这样:

List<Predicate> checks = Arrays.asList(myClassHelper::isCond1, myClassHelper::isCond2);

或者

List<Predicate> checks = Arrays.asList(a -> myClassHelper::isCond1, a -> myClassHelper::isCond2);

(这些都不正确)
我在哪里可以指定此处条件的参数?

抱歉,如果这个标题可以更好的话,我总是为此而挣扎。

I have a few predicates that I want to put in a list so I can then call stream().noneMatch() on my list. I successfully did this by creating named Predicates but how can I create them within an Arrays.asList()'s argument list?

Here's the working code that I'd like to convert:

ArrayList<Predicate<MyClass>> checks = new ArrayList<>();
Predicate<MyClass> isCond1 = myClassHelper::isCond1;
Predicate<MyClass> isCond2 = myClassHelper::isCond2;
checks.add(isCond1);
checks.add(isCond2);

I'd expect the result of the conversion to look something like this:

List<Predicate> checks = Arrays.asList(myClassHelper::isCond1, myClassHelper::isCond2);

or

List<Predicate> checks = Arrays.asList(a -> myClassHelper::isCond1, a -> myClassHelper::isCond2);

(neither of these are correct)
Where can I specify the argument for the conditions here?

Sorry if this could have been titled better, I always struggle with that.

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

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

发布评论

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

评论(2

高速公鹿 2025-01-18 13:51:21

第一条语句应该有效,但您忘记指定谓词类型 Predicate,否则编译器无法从方法引用中推断出它:

List<Predicate<MyClass>> checks2 = Arrays.asList(myClassHelper::isCond1, myClassHelper::isCond2);

或者使用 lambda:

List<Predicate<MyClass>> checks3 = Arrays.asList(a -> myClassHelper.isCond1(a), a -> myClassHelper.isCond2(a));

The first statement should work but you forgot to specify the predicate type Predicate<MyClass>, otherwise the compiler cannot infer it from the method references:

List<Predicate<MyClass>> checks2 = Arrays.asList(myClassHelper::isCond1, myClassHelper::isCond2);

Or using lambdas:

List<Predicate<MyClass>> checks3 = Arrays.asList(a -> myClassHelper.isCond1(a), a -> myClassHelper.isCond2(a));
小巷里的女流氓 2025-01-18 13:51:21

如果要将多个 Predicate 转换为

List

List.of 因为 JKD 9 是一个更好的 API(例如,返回列表是不可变的,拒绝空),检查 List.of 和 A​​rrays.asList 有什么区别?< /a> 了解详细信息。

Stream.of 甚至更好。

Predicate

如果有多个 MyClass 需要测试,将 Predicate 组合在一起会给你更多的灵活性,并且看起来更自然。

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class MultiplePredicate {
    public static void main(String[] args) {
        List<MyClass> items = new ArrayList<>();
        items.add(new MyClass(1));
        items.add(new MyClass(2));
        items.add(new MyClass(3));
        //                                          drawback as compiler not smart enough
        Predicate<MyClass> allCondNotMatch = Stream.<Predicate<MyClass>>of(MyClassHelper::isCond1, MyClassHelper::isCond2)
                .map(Predicate::not)
                .reduce((myClass) -> true, Predicate::and);
        List<MyClass> itemsWithAllCondNotMatch = items.stream().filter(allCondNotMatch).toList();
        System.out.println(itemsWithAllCondNotMatch);
    }

    public static class MyClassHelper {
        public static boolean isCond1(MyClass myClass) {
            return myClass.value == 1;
        }

        public static boolean isCond2(MyClass myClass) {
            return myClass.value == 2;
        }
    }

    public static class MyClass {
        private final int value;

        public MyClass(int value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "MyClass{" +
                    "value=" + value +
                    '}';
        }
    }
}

If you want to convert multiple Predicate to

List

List.of since JKD 9 is a better API(e.g the return list is immutable, rejecting null), check What is the difference between List.of and Arrays.asList? for details.

Stream

Stream.of is even better if you actually want to use Stream API.

Predicate

If there are multiple MyClass to test, combining the Predicate to one give your more flexibility, and looks more natural.

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class MultiplePredicate {
    public static void main(String[] args) {
        List<MyClass> items = new ArrayList<>();
        items.add(new MyClass(1));
        items.add(new MyClass(2));
        items.add(new MyClass(3));
        //                                          drawback as compiler not smart enough
        Predicate<MyClass> allCondNotMatch = Stream.<Predicate<MyClass>>of(MyClassHelper::isCond1, MyClassHelper::isCond2)
                .map(Predicate::not)
                .reduce((myClass) -> true, Predicate::and);
        List<MyClass> itemsWithAllCondNotMatch = items.stream().filter(allCondNotMatch).toList();
        System.out.println(itemsWithAllCondNotMatch);
    }

    public static class MyClassHelper {
        public static boolean isCond1(MyClass myClass) {
            return myClass.value == 1;
        }

        public static boolean isCond2(MyClass myClass) {
            return myClass.value == 2;
        }
    }

    public static class MyClass {
        private final int value;

        public MyClass(int value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "MyClass{" +
                    "value=" + value +
                    '}';
        }
    }
}

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