What is EnumSet
What is EnumSet

What is EnumSet

Answer: In java EnumSet is a class which extends AbstractSet implements Set. This class use with the keys that should be enum type.

EnumSet is a generic class with below detail:

class EnumSet<E>

Where, E is specifying elements and also take notice that E must extend Enum which actually enforces the requirement of enum type.

It defined no constructor EnumSet defines no constructors and it factory methods to to create objects. Below is example class:

package com.javahonk.hashsettest;

import java.util.EnumSet;

public class EnumSetTest {

	public static final void main(String args[]) {

	System.out.println("All season list: ");

	for (Season season : Season.ALL_SEASON)
		System.out.println(season);

	System.out.println("\nAfter spring season list:");
	for (Season season : Season.AFTER_SPRING_SEASON)
		System.out.println(season);		

	}
}

enum Season {

	SPRING,SUMMER, AUTUMN, WINTER;

	public static final EnumSet<Season> AFTER_SPRING_SEASON
			= EnumSet.range(SUMMER,WINTER);

	public static final EnumSet<Season> ALL_SEASON 
			= EnumSet.allOf(Season.class);
}

 

Output:

What is EnumSet

Leave a Reply

Your email address will not be published. Required fields are marked *