java.util EnumSet is a Java library that provides a specialized set implementation for use with enum types. It is a high-performance data structure that is designed to store sets of enum constants efficiently.
One of the methods provided by EnumSet is copyOf(), which allows you to create a new EnumSet that is a copy of an existing collection. Here are some examples of how this method can be used:
Example 1: Copying a Set of Enum Constants Suppose you have an existing Set of enum constants and you want to create a new EnumSet that contains the same elements. You can do this by passing the Set to the copyOf() method:
Set mySet = new HashSet<>(Arrays.asList(MyEnum.VALUE1, MyEnum.VALUE2, MyEnum.VALUE3)); EnumSet enumSet = EnumSet.copyOf(mySet);
Example 2: Copying a Sub-Range of Enum Constants You can also use the copyOf() method to create a new EnumSet that contains a sub-range of the values defined by the enum type. In this example, we create a new EnumSet that contains only the first two values of the MyEnum enum type:
Example 3: Copying an Array of Enum Constants Finally, you can use the copyOf() method to create a new EnumSet that contains the elements of an array of enum constants:
The EnumSet class is part of the java.util package in the Java standard library.
Java EnumSet.copyOf - 30 examples found. These are the top rated real world Java examples of java.util.EnumSet.copyOf extracted from open source projects. You can rate examples to help us improve the quality of examples.