- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java EnumSet copyOf() Method
Description
The Java EnumSet copyOf(Collection<E> c) method creates an enum set initialized from the specified collection.
Declaration
Following is the declaration for java.util.EnumSet.copyOf() method
public static <E extends Enum<E>> EnumSet<E>> copyOf(Collection<E> c)
Parameters
c − the collection from which to initialize this enum set.
Return Value
This method does not return any value.
Exception
IllegalArgumentException − if c is not an EnumSet instance and contains no elements
NullPointerException − if c is null
Getting Copy of EnumSet Example
The following example shows the usage of Java EnumSet copyOf(Collection) method to populate the EnumSet instance. We've created a enum Numbers. Then a Collection instance is created and populated with enum values. Using copyOf(Collection) method, enumSet is populated with values of collection and resulted enumSet is printed.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Collection; import java.util.EnumSet; public class EnumSetDemo { // create an enum public enum Numbers { ONE, TWO, THREE, FOUR, FIVE }; public static void main(String[] args) { // create a new collection Collection<Numbers> collection = new ArrayList<>(); // print the collection System.out.println("Colletion :" + collection); // add two elements in the collection collection.add(Numbers.ONE); collection.add(Numbers.THREE); // create an EnumSet that is a copy of the collection EnumSet<Numbers> set = EnumSet.copyOf(collection); // print the set System.out.println("Set:" + set); } }
Output
Let us compile and run the above program, this will produce the following result −
Colletion :[] Set:[ONE, THREE]
Getting Copy of EnumSet of Enum with Value Example
The following example shows the usage of Java EnumSet copyOf(Collection) method to populate the EnumSet instance. We've created a enum Numbers. Then a Collection instance is created and populated with enum values. Using copyOf(Collection) method, enumSet is populated with values of collection and resulted enumSet is printed.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Collection; import java.util.EnumSet; public class EnumSetDemo { // create an enum public enum Number { ONE("1"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5"); // instance variable for enum private String value; public String getValue() { return this.value; } // constructor should be private or protected private Number(String value) { this.value = value; } @Override public String toString() { return this.name() +"(" + this.value + ")"; } }; public static void main(String[] args) { // create a new collection Collection<Number> collection = new ArrayList<>(); // print the collection System.out.println("Colletion :" + collection); // add two elements in the collection collection.add(Number.ONE); collection.add(Number.THREE); // create an EnumSet that is a copy of the collection EnumSet<Number> set = EnumSet.copyOf(collection); // print the set System.out.println("Set:" + set); } }
Output
Let us compile and run the above program, this will produce the following result −
Colletion :[] Set:[ONE(1), THREE(3)]
To Continue Learning Please Login
Login with Google