Home »
Java »
Java Programs
Java program to get the size of the EnumSet collection
Given an EnumSet collection, we have to get the size of it.
Submitted by Nidhi, on May 29, 2022
Problem statement
In this program, we will create an Enum for COLORS constants. Then we will create an EnumSet collection and add elements to it. After that, we will get the size of the EnumSet collection using the size() method.
Source Code
The source code to get the size of the EnumSet collection is given below. The given program is compiled and executed successfully.
// Java program to get the size of the
// 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 = EnumSet.noneOf(COLORS.class);
enumSet.add(COLORS.RED);
enumSet.add(COLORS.GREEN);
enumSet.add(COLORS.BLUE);
System.out.println("Size of enumSet is: " + enumSet.size());
}
}
Output
Size of enumSet is: 3
Explanation
Here, we created an EnumSet collection and add elements to it. Then we got the size of enumSet and printed the result.
Java EnumSet Programs »