13

I have an enum :

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

And then I have a list of 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) || ....

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.

2
  • 2
    try !Collections.disjoint(list1, list2); to check if list2 contains elements form list2
    – shahaf
    Commented Apr 11, 2018 at 10:20
  • 1
    What exactly do you mean with "I want to check if my list (of enums) has at least one of the enums". Maybe I'm misunderstanding, but if you have a List<PermissionEnum>, aren't all elements one of those enums? Aside from that, the best way to store a collection of enums is in an EnumSet, because it doesn't make sense to have the same permission twice or more. Commented Apr 11, 2018 at 10:42

4 Answers 4

17

Collections.disjoint returns true if the two specified collections have no elements in common. If it returns false, then your list has at least one of the enums.

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

A Stream API approach could be:

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

(similar to an iterative approach but with parallelisation included)

1
  • 6
    Instead of asList(PermissionsEnum.values()), I’d use EnumSet.allOf(PermissionsEnum.class) which returns a Set having O(1) lookup and further doesn’t need an array copy of all values. Further, I’d not be so quick in inserting parallelStream(), as it’s rather unlikely that the list is so humongous that performing such a trivial operation in parallel has a benefit.
    – Holger
    Commented Apr 11, 2018 at 11:42
6

You can use Collections.disjoint().

Create another List with the enums you want to check and then do Collections.disjoint(list, listOfEnumsToCheck). It returns true if no elements are found. If it is false at least one element is present.

I guess you can even use Enum.values() so it will become:

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

If you can use 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.

In other words it may return true as soon as element which satisfies predicate is found.

Apparently, more efficient way is to use EnumSet.allOf(PermissionsEnum.class)::contains because EnumSet.contains is much more efficient than List.contains:

list.stream().anyMatch(EnumSet.allOf(PermissionsEnum.class)::contains)
2
  • 2
    list.stream().anyMatch(EnumSet.allOf(PermissionsEnum.class)::contains) is preferable as EnumSet.contains is much more efficient than List.contains for all common List implementations…
    – Holger
    Commented Apr 11, 2018 at 11:45
  • @Holger, thanks a lot for your comment, I didn't know that, will update my answer. Commented Apr 11, 2018 at 12:38
0

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. 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");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.