简体   繁体   English

如何在方法中流式传输Java List(Varargs)的值?

[英]How to stream value of Java List (Varargs) in a method?

I have the following method: 我有以下方法:

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

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: 以下是A类的属性:

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

If you want to make sure all the items are not included in the exclusions you could do: 如果您想确保所有项目都不包含在exclusions项中,您可以执行以下操作:

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

Which will create a Stream of exclusions and then use noneMatch() to ensure the given AType is not included in the Array 这将创建一个exclusions Stream ,然后使用noneMatch()来确保给定的AType不包含在Array

You should rethink whether List really is the appropriate data type for something containing unique elements. 您应该重新考虑List是否确实是包含唯一元素的内容的适当数据类型。 A Set usually is more appropriate. Set通常更合适。

Then, if you care for performance, you may implement it as 然后,如果您关心性能,可以将其实现为

public static Set<A> getValuesExclusion(A... exclusions){
    return exclusions.length == 0? EnumSet.allOf(A.class):
        EnumSet.complementOf(EnumSet.of(exclusions[0], exclusions));
}

The class EnumSet is specifically designed for holding elements of an enum type, just storing a bit for each constant, to tell whether it is present or absent. EnumSet类专门用于保存enum类型的元素,只为每个常量存储一个位,以判断它是存在还是不存在。 This allows operations like complementOf , which just flips all bits using a single ⟨binary not⟩ operation, without the need to actually traverse the enum constants. 这允许像complementOf这样的操作,它只使用单个⟨binarynot⟩操作翻转所有位,而不需要实际遍历enum常量。

If you insist on returning a List , you can do it as easy as 如果你坚持要返回一个List ,你就可以这么简单

public static List<A> getValuesExclusion(A... exclusions){
    return new ArrayList<>(exclusions.length == 0? EnumSet.allOf(A.class):
        EnumSet.complementOf(EnumSet.of(exclusions[0], exclusions)));
}

I would not go with Stream s here but with the a (imho) more readable approach: 我不会在这里使用Stream ,而是使用(imho)更具可读性的方法:

public static List<A> getValuesExclusion(AType... exclusions){
    List<A> values = Arrays.asList(values());
    values.removeAll(Arrays.asList(ex));
    return values;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM