×

Java Programs

Java Practice

Java program to create an empty EnumSet using noneOf() method

Java example to create an empty EnumSet using noneOf() method.
Submitted by Nidhi, on May 28, 2022

Problem statement

In this program, we will create an Enum for COLORS constants. Then we will create two EnumSet collections. Here, we will create an empty EnumSet using the noneOf() method.

Java program to create an empty EnumSet using noneOf() method

The source code to create an empty EnumSet using the noneOf() method is given below. The given program is compiled and executed successfully.

// Java program to create an empty EnumSet 
// using noneOf() method

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.allOf(COLORS.class);

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

    EnumSet < COLORS > emptySet = EnumSet.noneOf(COLORS.class);

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

Output

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

Explanation

The Main class contains a main() method. The main() method is the entry point for the program. And, created a reference of the EnumSet collection and initialized it with elements of COLORS elements using the allOf() method. Here we also created an empty EnumSet using the noneOf() method and printed the empty EnumSet.

Java EnumSet Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.