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>> || This is the class decralation of enum set. EnumSet<e extends enum>> enum1, enum2, enum3, enum4; || Declare different enum.</e></e>
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));</enum_type></t>
Methods of Enumset in Java
-
static
> allOf(Class This method is used to create the enum with all elements mentioned.elementType): -
EnumSet
clone(): This method is used to create the copy of the set. Return type is EnumSet. -
static
> complementOf(EnumSet This method is used to create the enum set specified in the method , this enum will contain all the elements which are not contained in set.s): -
static
> copyOf(Collection This will create a copy of enum set which is specified in the method. But it takes the collection parameter.c): -
static
> copyOf(EnumSet This will create a enum, specified as enum set. This will return an EnumSets): . -
static
> noneOf(Class This will also create an enum which specified in the method. Return an EnumSetelementType): . -
static
> of(E e): Also create an enum with initially elements. Return type is EnumSet. -
static
> of(E first, E… rest): Creates enum containing elements and it returns EnumSet -
static
> of(E e1, E e2): This method will create an enum with mentioned elements. Return type is EnumSet -
static
> of(E e1, E e2, E e3): This method will create an enum. Method return type is EnumSet. -
static
> of(E e1, E e2, E e3, E e4): This methods will also create the enum with specified elements. Return type is EnumSet -
static
> of(E e1, E e2, E e3, E e4, E e5): This method is also create the enum with specifies elements passed as enum. Return type is EnumSet -
static
> range(E from, E to): In this method we can specified the range. Then it will create a new enum. Return type is EnumSet.
Note:
- Enum set implements various class and interface. Enum set class declaration :
- public abstract class EnumSet
> extends AbstractSet implements Cloneable, Serializable
Interfaces which are implemented by enum set:
- Cloneable
- Set
- Serializable
- Collection
- Iterable
Classes that are extending by enum set are the following:
- AbstractSet
Examples of Enumset in Java
Following are the example of enumset are given below:
Example #1
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 };</enumdemo>
Output:
Example #2
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 };</enumdemo></enumdemo>
Output:
Example #3
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 };</enumdemo></enumdemo>
Output:
Example #4
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 };</enumdemo></enumdemo>
Output:
Example #5
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 };</enumdemo></enumdemo>
Output:
Conclusion
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!

Java is platform-independent because of its "write once, run everywhere" design philosophy, which relies on Java virtual machines (JVMs) and bytecode. 1) Java code is compiled into bytecode, interpreted by the JVM or compiled on the fly locally. 2) Pay attention to library dependencies, performance differences and environment configuration. 3) Using standard libraries, cross-platform testing and version management is the best practice to ensure platform independence.

Java'splatformindependenceisnotsimple;itinvolvescomplexities.1)JVMcompatibilitymustbeensuredacrossplatforms.2)Nativelibrariesandsystemcallsneedcarefulhandling.3)Dependenciesandlibrariesrequirecross-platformcompatibility.4)Performanceoptimizationacros

Java'splatformindependencebenefitswebapplicationsbyallowingcodetorunonanysystemwithaJVM,simplifyingdeploymentandscaling.Itenables:1)easydeploymentacrossdifferentservers,2)seamlessscalingacrosscloudplatforms,and3)consistentdevelopmenttodeploymentproce

TheJVMistheruntimeenvironmentforexecutingJavabytecode,crucialforJava's"writeonce,runanywhere"capability.Itmanagesmemory,executesthreads,andensuressecurity,makingitessentialforJavadeveloperstounderstandforefficientandrobustapplicationdevelop

Javaremainsatopchoicefordevelopersduetoitsplatformindependence,object-orienteddesign,strongtyping,automaticmemorymanagement,andcomprehensivestandardlibrary.ThesefeaturesmakeJavaversatileandpowerful,suitableforawiderangeofapplications,despitesomechall

Java'splatformindependencemeansdeveloperscanwritecodeonceandrunitonanydevicewithoutrecompiling.ThisisachievedthroughtheJavaVirtualMachine(JVM),whichtranslatesbytecodeintomachine-specificinstructions,allowinguniversalcompatibilityacrossplatforms.Howev

To set up the JVM, you need to follow the following steps: 1) Download and install the JDK, 2) Set environment variables, 3) Verify the installation, 4) Set the IDE, 5) Test the runner program. Setting up a JVM is not just about making it work, it also involves optimizing memory allocation, garbage collection, performance tuning, and error handling to ensure optimal operation.

ToensureJavaplatformindependence,followthesesteps:1)CompileandrunyourapplicationonmultipleplatformsusingdifferentOSandJVMversions.2)UtilizeCI/CDpipelineslikeJenkinsorGitHubActionsforautomatedcross-platformtesting.3)Usecross-platformtestingframeworkss


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
