Home >Java >javaTutorial >Enumset in Java
Enum set are the implementation of a set interface, and it extends Abstract class in java for enumeration type. The are Set collection provides the functionality to work with enums in java. Enum sets are not synchronized; hence they provide faster accessing of elements. Enum sets do not allow adding null elements; if we try to add a null value, it will throw a null pointer exception.
Syntax:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
public abstract class EnumSet<E extends Enum<E>> || This is the class decralation of enum set. EnumSet<E extends Enum<E>> enum1, enum2, enum3, enum4; || Declare different enum.
As enum sets are not synchronized, they are much master, but we can synchronise them by using collections class synchronized method() below find the syntax for synchronized.
Collections.synchronizedSet(java.util.Set<T>) Set<Enum_Type> s = Collections.synchronizedSet(EnumSet.noneOf(EnumClass.class));
Note:
Interfaces which are implemented by enum set:
Classes that are extending by enum set are the following:
Following are the example of enumset are given below:
In the below example, w are adding elements to the enum set.
Code:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
Output:
In this example, we are creating enum2 by using the value sf enum1 only. Below find an example.
Code:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); // adding value to another enum set using addAll method EnumSet<EnumDemo> enum2 = EnumSet.allOf(EnumDemo.class); System.out.println("Enum set 2 containing values are :: "); System.out.println( enum2); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
Output:
In this example, we are finding complements f enum1 to enum2.
Code:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); // finind completents of enum1 to enum2 EnumSet<EnumDemo> enum2 = EnumSet.complementOf(enum1); System.out.println("Enum set 2 containing values are :: "); System.out.println( enum2); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
Output:
Specifying range to elements to be copied.
Code:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); // here we are specifing the range to another enum using enum1 EnumSet<EnumDemo> enum2 = EnumSet.range(EnumDemo.PYTHON, EnumDemo.JAVASCRIPT); System.out.println("Enum set 2 containing values are :: "); System.out.println( enum2); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
Output:
Making copy from existing enum.
Code:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); // here we are copying elements of enum1 to enum2. EnumSet<EnumDemo> enum2 = EnumSet.copyOf(enum1); System.out.println("Enum set 2 containing values are :: "); System.out.println( enum2); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
Output:
Java EnumSet is the implementation of the SET interface in java. They are a special type of java collection framework that provides support for enum types in java. We can also make them synchronized as these are not synchronized.
The above is the detailed content of Enumset in Java. For more information, please follow other related articles on the PHP Chinese website!