ADVERTISEMENT
ADVERTISEMENT

Java program to create an EnumSet collection

Java example to create an EnumSet collection.
Submitted by Nidhi, on May 27, 2022

Problem Solution:

In this program, we will create an Enum for COLORS constants. Then we will create a set of enum constants using the EnumSet collection.

Program/Source Code:

The source code to create an EnumSet collection is given below. The given program is compiled and executed successfully.

// Java program to create an EnumSet collection

import java.util.*;

//Enum for color constants
enum COLORS {
  RED,
  GREEN,
  BLUE,
  BLACK,
  WHITE
};

public class Main {
  public static void main(String[] args) {
    EnumSet < COLORS > enumSet;

    //Adding elements to EnumSet.
    enumSet = EnumSet.of(COLORS.BLUE, COLORS.BLACK, COLORS.GREEN, COLORS.WHITE);

    System.out.println("EnumSet is: " + enumSet);
  }
}

Output:

EnumSet is: [GREEN, BLUE, BLACK, WHITE]

Explanation:

The Main class contains a main() method. The main() method is the entry point for the program. And, created a reference of EnumSet collection and initialized it with of() method with constants of the enum. After that, we printed the created enum.

Java EnumSet Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.