简体   繁体   English

检查列表是否至少包含另一个 - 枚举

[英]Check if list contains at least one of another - enums

I have an enum : 我有一个枚举:

public enum PermissionsEnum {
    ABC("Abc"),        
    XYZ("Xyz"),
    ....
}

And then I have a list of Enums . 然后我有一个Enums列表。 I want to check if my list has at least one of the enums. 我想检查一下我的列表中是否至少有一个枚举。 I currently check it by an iterative approach. 我目前通过迭代方法检查它。 I also know there is a way to do it by using || 我也知道有一种方法可以通过使用|| checking list.contains(enum.ABC..) || list.contains(enum.XYZ) || ... 检查list.contains(enum.ABC..) || list.contains(enum.XYZ) || ... list.contains(enum.ABC..) || list.contains(enum.XYZ) || ... list.contains(enum.ABC..) || list.contains(enum.XYZ) || ... . list.contains(enum.ABC..) || list.contains(enum.XYZ) || ...

Is there a better way to do it? 有没有更好的方法呢?

This question shows how to do it if the objective list is a list of Strings, I want to get the matching status if the list is another list of enums. 此问题显示如果目标列表是字符串列表如何执行操作,如果列表是另一个枚举列表,我想获得匹配状态。

Collections.disjoint returns true if the two specified collections have no elements in common. 如果两个指定的集合没有共同的元素,则Collections.disjoint返回true If it returns false , then your list has at least one of the enums. 如果它返回false ,那么您的列表至少包含一个枚举。

boolean contains = !Collections.disjoint(list, EnumSet.allOf(PermissionsEnum.class)));

A Stream API approach could be: Stream API方法可以是:

EnumSet<PermissionsEnum> set = EnumSet.allOf(PermissionsEnum.class);
boolean contains = list.stream().anyMatch(set::contains);

(similar to an iterative approach but with parallelisation included) (类似于迭代方法,但包含并行化)

You can use Collections.disjoint() . 您可以使用Collections.disjoint() Create another List with the enums you want to check and then do Collections.disjoint(list, listOfEnumsToCheck) - it return true if no elements are found. 使用要检查的枚举创建另一个List,然后执行Collections.disjoint(list, listOfEnumsToCheck) - 如果未找到任何元素,则返回true。 If it is false at least one element is present. 如果为假,则至少存在一个元素。

I guess you can even use Enum.values() so it will become: 我猜你甚至可以使用Enum.values(),所以它会变成:

if(!Collections.disjoint(list, PermissionsEnum.values()) { doStuff } //Using ! to show there is at least one value

If you can use java-8 . 如果你可以使用java-8

Arrays.stream(PermissionsEnum.values()).anyMatch(list::contains);

Enum#values()

Returns an array containing the constants of this enum type. 返回一个包含此枚举类型常量的数组。

So we just need to wrap it into a stream and check if list contains any values. 所以我们只需要将它包装成一个流并检查列表是否包含任何值。 Important thing to remember is that 要记住的重要一点是

anyMatch not evaluate the predicate on all elements if not necessary for determining the result. 如果不需要确定结果,anyMatch不会评估所有元素的谓词。

In other words it may return true as soon as element which satisfies predicate is found. 换句话说,只要找到满足谓词的元素,它就可以返回true。

Apparently, more efficient way is to use EnumSet.allOf(PermissionsEnum.class)::contains because EnumSet.contains is much more efficient than List.contains : 显然,更有效的方法是使用EnumSet.allOf(PermissionsEnum.class)::contains因为EnumSet.containsList.contains更有效:

list.stream().anyMatch(EnumSet.allOf(PermissionsEnum.class)::contains)

you could have a static list in a Type Safe Enum Pattern enum to act as the master list, which is added to in the enum constructor. 您可以在Type Safe Enum Pattern枚举中使用静态列表作为主列表,该列表将添加到枚举构造函数中。 That way your master list always contains exactly one of each enum, and you can compare to that. 这样你的主列表总是包含每个枚举中的一个,你可以与之比较。

import java.util.ArrayList;

public class EnumeratedEnum {
    public static final ArrayList<EnumeratedEnum> Members=new ArrayList<>();
    private final String value;
    public String getValue() {return value;}
    private EnumeratedEnum(String value) {
        this.value=value;
        Members.add(this);
    }
    public static final EnumeratedEnum EnumOne=new EnumeratedEnum("someValue");
    public static final EnumeratedEnum EnumTwo=new EnumeratedEnum("someValue2");
    public static final EnumeratedEnum EnumThree=new EnumeratedEnum("someValue3");
    public static final EnumeratedEnum EnumFour=new EnumeratedEnum("someValue4");
    public static final EnumeratedEnum EnumFive=new EnumeratedEnum("someValue5");
}

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

相关问题 如何检查一个树集至少包含另一个树集中的一个项目 - How to check a treeset contains at least one items from another treeset 一个列表至少包含另一个列表中的一个值(Java 8) - A list contains at least one value from another list (Java 8) 检查列表是否至少包含一个枚举的最佳方法 - Best way to check if list contains at least one of an enum Java流 - 验证列表中至少有一个元素包含在另一个元素中 - Java stream - verify at least one element in a list contains in another 检查枚举列表是否包含Java中的字符串 - Check if a list of Enums contains a String in Java 如何检查一个列表的 object 是否包含另一个列表的任何 object - How to check if an object of one list contains any object of another list 如何检查一个字符串是否至少包含另一个字符串中的一个字符? - How do I check if a string contains at least one character from another string? 多对多检查一个列表是否包含另一个列表的元素 - Many to many check if one list contains elements of another 有没有办法用java递归检查字符串是否包含至少一个大写字母? - Is there a way to check if a string contains at least one capital letter recursively with java? 使用正则表达式检查一个字符串至少包含一个Unicode字母 - Check a string contains at least one Unicode letter using regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM