[英]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.contains
比List.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.