如何在方法中流式传输Java List(Varargs)的值? [英] How to stream value of Java List (Varargs) in a method?

查看:47
本文介绍了如何在方法中流式传输Java List(Varargs)的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法:

public static List<A> getValuesExclusion(A exclusion) {
        return Arrays.stream(values())
                .filter(item -> item != exclusion)
                .collect(Collectors.toList());
}
//this function returns enum list of A types that has no A type'exclusion'

现在,我想将其作为参数添加到列表中:

Now I want to make it into a list as argument:

public static List<A> getValuesExclusion(A... exclusions){
        return Arrays.stream(values())
                .filter(???)
                .collect(Collectors.toList());
}

我的问题是,如何为第二种情况做过滤器?我想检索一个枚举列表,该列表排除所有值"exclusions"作为输入.这是类A的属性:

My question is, how can I do the filter for the second case? I would like to retrieve an enum list that excludes all the values "exclusions" as input. Here are the attributes of class A:

public enum A implements multilingualA{
    A("a"),
    B("b"),
    C("c"),
    D("d");
    ...
}

推荐答案

如果要确保所有项目都未包含在 exclusions 中,则可以执行以下操作:

If you want to make sure all the items are not included in the exclusions you could do:

public static List<A> getValuesExclusion(AType... exclusions){
        return Arrays.stream(values())
                .filter(e -> Arrays.stream(exclusions).noneMatch(c -> c == e))
                .collect(Collectors.toList());
}

这将创建一个 exclusions Stream ,然后使用 noneMatch()确保给定的 AType 为不包含在 Array

Which will create a Stream of exclusions and then use noneMatch() to ensure the given AType is not included in the Array

这篇关于如何在方法中流式传输Java List(Varargs)的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆