Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

enumset
java
communitycreator

What is the EnumSet.containsAll method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

EnumSet is similar to Set, except that EnumSet only contains the Enum type as elements. Also, all the elements must be from a single enum type. For further details, click here.

The containsAll() method will check if all the Enum elements of the passed collection are present in the EnumSet object.

Syntax

boolean containsAll(Collection<?> c)

This method takes the collection to be checked to see if it is present in this set.

This method returns true if all the Enum elements of the passed collection are present in the EnumSet object.

Code

import java.util.EnumSet;
import java.util.ArrayList;
class containsAll {
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
public static void main( String args[] ) {
EnumSet<Size> set = EnumSet.of(Size.SMALL, Size.MEDIUM);
ArrayList<Size> list1 = new ArrayList<>();
list1.add(Size.SMALL);
list1.add(Size.MEDIUM);
System.out.println("The set is "+ set);
System.out.println("list1 is "+ list1);
System.out.println("\nIf set contains list1 is "+ set.containsAll(list1));
ArrayList<Size> list2 = new ArrayList<>();
list2.add(Size.LARGE);
System.out.println("\nlist2 is "+ list2);
System.out.println("If set contains list2 is "+ set.containsAll(list2));
}
}

In the above code:

  • In lines 1 and 2, we imported the EnumSet and ArrayList classes.

  • In line 4, we created an Enum with the name Size.

  • In lines 8 and 9, we created a new EnumSet object using the of method. We passed Size.SMALL and Size.MEDIUM as an argument to the of() method. The of() method will return an EnumSet object that contains Size.SMALL and Size.MEDIUM enum elements.

  • In line 10, we created a new ArrayList object with name list1 and added the Size.SMALL and Size.MEDIUM element to it.

  • In line 16, we used the containsAll method to check if all elements of list1 are present in set. In this case, true is returned because all the elements of list1 are present in set.

  • In line 18, we created a new ArrayList object with name list2 and added the Size.LARGE element to it.

  • In line 22, we used the containsAll() method to check if all elements of list2 are present in the set. In this case, false is returned because the element Size.LARGE of list2 is not present in set.

RELATED TAGS

enumset
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring