×

Java Programs

Java Practice

Java program to check whether an EnumSet collection is empty or not

Given an EnumSet collection, we have to check whether it is empty or not.
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 and check created Enum Sets are empty or not using the isEmpty() method.

Java program to check whether an EnumSet collection is empty or not

The source code to check whether an EnumSet collection is empty or not is given below. The given program is compiled and executed successfully.

// Java program to check whether an EnumSet collection 
// is empty or not

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 > enumSet1;
    EnumSet < COLORS > enumSet2 = EnumSet.noneOf(COLORS.class);

    enumSet1 = EnumSet.range(COLORS.GREEN, COLORS.BLACK);

    if (enumSet1.isEmpty())
      System.out.println("The enumSet1 is an empty EnumSet collection.");
    else
      System.out.println("The enumSet1 is not an empty EnumSet collection.");

    if (enumSet2.isEmpty())
      System.out.println("The enumSet2 is an empty EnumSet collection.");
    else
      System.out.println("The enumSet2 is not an empty EnumSet collection.");
  }
}

Output

The enumSet1 is not an empty EnumSet collection.
The enumSet2 is an empty EnumSet collection.

Explanation

The Main class contains a main() method. The main() method is the entry point for the program. And, created two EnumSet collections enumSet1, enumSet2. Then we checked whether created Enum Sets are empty or not using the isEmpty() method and printed the appropriate method.

Java EnumSet Programs »





Comments and Discussions!

Load comments ↻





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