Java is one of the most popular programming languages used to build a wide variety of applications. While building applications, we often use enumerations to serve a group of named constants. But, if you want to implement a Set interface with the enumeration type, then you have to use the EnumSet in Java. In this article on Java EnumSet, I will be covering the following topics:
What is Java EnumSet?
EnumSet is an implementation of the Set collections to work with the enum type. EnumSet extends from the AbstractSet and implements the Set interface. The following are a few important points you need to understand about EnumSet in Java:
- Contains only enum values which belong to the same enumeration type
- It is a member of the Java collections framework
- Provides high performance set implementation and is not synchronized
- It does not allow the user to add NULL values and throws a NullPointerException
- Elements are stored in the order of which they are saved
- Uses a fail-safe iteration, which can be used to make sure that ConcurrentModificationException is thrown
You can declare Java EnumSet in the following manner:
Declaration
public abstract class EnumSet<E extends Enum<E>>
Next, in this article on Java EnumSet, let us understand the different methods offered by this class.
Methods of EnumSet
The various methods offered by Java EnumSet are as follows:
Method | Modifier and Type | Description |
of(E e1) | static <E extends Enum<E>> EnumSet<E> | Used to create an enum set initially containing the mentioned element i.e. e1. |
of(E e1, E e2) | static <E extends Enum<E>> EnumSet<E> | Used to create an enum set initially containing the mentioned elements. Here, it is e1, e2. |
range(E from, E to) | static <E extends Enum<E>> EnumSet<E> | Used to create an enum set initially containing all of the elements in the range defined by the two mentioned endpoints. |
allOf(Class<E> elementType) | static <E extends Enum<E>> EnumSet<E> | Used to create an enum set containing all of the elements in the menioned element type. |
copyOf(Collection<E> c) | static <E extends Enum<E>> EnumSet<E> | Used to create an enum set initialized from the mentioned collection. |
copyOf(EnumSet<E> s) | static <E extends Enum<E>> EnumSet<E> | Used to create an enum set with the same element type as the mentioned enum set, initially containing the same elements (if there any present). |
complementOf(EnumSet<E> s) | static <E extends Enum<E>> EnumSet<E> | Used to create an enum set with the same element type as the mentioned enum set, initially containing all the elements of this type that are not contained in the specified set. |
noneOf(Class<E> elementType) | static <E extends Enum<E>> EnumSet<E> | Used tp create an empty enum set with the specified element type. |
clone() | EnumSet<E> | Used to return a copy of this set. |
Note: You can use the of() method up to 5 parameters. So, you can create an enum set initially containing the specified elements as follows:
- of(E e1, E e2, E e3)
- of(E e1, E e2, E e3, E e4)
- of(E e1, E e2, E e3, E e4, E e5)
Since, I have discussed the methods used with EnumSet, next in Java EnumSet tutorial, let us see the practical operations of these methods.
Operations of Java EnumSet
To explain you the operations of EnumSet, I will consider the following code snippet. This code snippet contains a set of enum values [DevOps, Big Data, Python, Data Science, RPA]. In the later section of the code, I will show you how to use different methods in the following sequence:
- of(E e1)
- of(E e1, E e2)
- of(E e1, E e2, E e3)
- of(E e1, E e2, E e3, E e4)
- of(E e1, E e2, E e3, E e4, E e5)
- range(E from, E to)
- allOf(Class<E> elementType)
- copyOf(Collection<E> c)
- copyOf(EnumSet<E> s)
- complementOf(EnumSet<E> s)
- noneOf(Class<E> elementType)
- clone()
Code Snippet:
package edureka; import java.util.ArrayList; import java.util.Collection; import java.util.EnumSet; enum Courses { DevOps, BigData, Python, DataScience, RPA }; public class Example { public static void main(String[] args) { // Create an EnumSet EnumSet<Courses> sample_set; //of method // Add single element sample_set = EnumSet.of(Courses.DevOps); // Display the set System.out.println("The EnumSet after adding a single element is: " + sample_set); // Add two elements sample_set = EnumSet.of(Courses.DevOps, Courses.BigData); // Display the set System.out.println("The EnumSet after adding two elements is: " + sample_set); // Add three elements sample_set = EnumSet.of(Courses.DevOps, Courses.BigData, Courses.Python); // Display the set System.out.println("The EnumSet after adding three elements is: " + sample_set); // Add four elements sample_set = EnumSet.of(Courses.DevOps, Courses.BigData, Courses.Python, Courses.DataScience); // Display the set System.out.println("The EnumSet after adding four elements is: " + sample_set); // Add five elements sample_set = EnumSet.of(Courses.DevOps, Courses.BigData, Courses.Python, Courses.DataScience,Courses.RPA); // Display the set System.out.println("The EnumSet after adding five elements is: " + sample_set); //Range method sample_set = EnumSet.range(Courses.BigData, Courses.DataScience); // Display the set System.out.println("The range of the EnumSet is: " + sample_set); //allOf method sample_set = EnumSet.allOf(Courses.class); // Display the set System.out.println("All the elements in the EnumSet are: " + sample_set); //copyOf(Collection) method // Create an empty collection Collection<Courses> samplecollection = new ArrayList<Courses>(); // Add elements to the samplecollection samplecollection.add(Courses.DevOps); samplecollection.add(Courses.BigData); samplecollection.add(Courses.Python); // Display the sample collection set System.out.println("Elements in the sample collection set are: " + samplecollection); //Create a new EnumSet to store the collection items EnumSet<Courses> final_enumset = EnumSet.copyOf(samplecollection); // Display the EnumSet System.out.println("Elements in the EnumSet are: " + final_enumset); //copyOf(EnumSet) method // Get all the elements from Courses EnumSet<Courses> example_set = EnumSet.allOf(Courses.class); // Display the initial EnumSet(sample_set) System.out.println("The elements in the initial EnumSet are: " + example_set); // Copy the elements from the above set EnumSet<Courses> final_set = EnumSet.copyOf(example_set); // Display the elements in the copied EnumSet System.out.println("The elements in the copied EnumSet are: " + final_set); //complementOf method //Sample Set sample_set = EnumSet.of(Courses.DevOps, Courses.BigData, Courses.Python); //Create an EnumSet EnumSet<Courses> complement_set; //Complement the above set complement_set = EnumSet.complementOf(sample_set); // Display the elements in the complement EnumSet System.out.println("The elements in the complement EnumSet are: " + complement_set); //noneOf method // Create empty set EnumSet<Courses> none_example_set = EnumSet.noneOf(Courses.class); // Display the elements in the set System.out.println("EnumSet consists of the elements: " + none_example_set); //clone method EnumSet<Courses> final_clone_set = sample_set.clone(); //Display the EnumSet System.out.println("The clone set consists of the elements:" + final_clone_set); } }
Output:
The EnumSet after adding a single element is: [DevOps] The EnumSet after adding two elements is: [DevOps, BigData] The EnumSet after adding three elements is: [DevOps, BigData, Python] The EnumSet after adding four elements is: [DevOps, BigData, Python, DataScience] The EnumSet after adding five elements is: [DevOps, BigData, Python, DataScience, RPA] The range of the EnumSet is: [BigData, Python, DataScience] All the elements in the EnumSet are: [DevOps, BigData, Python, DataScience, RPA] Elements in the sample collection set are: [DevOps, BigData, Python] Elements in the EnumSet are: [DevOps, BigData, Python] The elements in the initial EnumSet are: [DevOps, BigData, Python, DataScience, RPA] The elements in the copied EnumSet are: [DevOps, BigData, Python, DataScience, RPA] The elements in the complement EnumSet are: [DataScience, RPA] EnumSet consists of the elements: [] The clone set consists of the elements:[DevOps, BigData, Python]
This brings us to the end of this article on Java EnumSet. If you want to know more about Java you can refer to our other Java Blogs.
If you found this article on “Java EnumSet” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
If you come across any questions, feel free to ask all your questions in the comments section of “Java EnumSet” and our team will be glad to answer.