The Java EnumSet class is a specialized Set implementation that is designed to work with enums. Enums are special data types that allow developers to define a set of named constants that have a specific integer value associated with them.
The EnumSet class is part of the Java Collections Framework and provides a high-performance, type-safe, and compact way to represent a set of enum values. It is optimized for working with enum types and provides a rich set of methods for performing set operations such as union, intersection, and complement.
Here is an example of how to use EnumSet in Java:
public enum Color { RED, GREEN, BLUE } public static void main(String[] args) { EnumSet<Color> colors = EnumSet.of(Color.RED, Color.GREEN); System.out.println(colors); // Output: [RED, GREEN] colors.add(Color.BLUE); System.out.println(colors); // Output: [RED, GREEN, BLUE] colors.remove(Color.RED); System.out.println(colors); // Output: [GREEN, BLUE] }
In the example above, we define an enum called Color
with three constants: RED
, GREEN
, and BLUE
. We then create an EnumSet called colors
and add RED
and GREEN
to it using the of
method. We print the contents of colors
using System.out.println
, which outputs [RED, GREEN]
.
We then add BLUE
to colors
using the add
method and print the contents again. This time, the output is [RED, GREEN, BLUE]
.
Finally, we remove RED
from colors
using the remove
method and print the contents once again. This time, the output is [GREEN, BLUE]
.
EnumSet class hierarchy:
The EnumSet class is part of the Java Collections Framework and is a concrete implementation of the Set interface. It is optimized for working with enum types and provides a rich set of methods for performing set operations such as union, intersection, and complement.
The class hierarchy of EnumSet is as follows:
java.lang.Object java.util.AbstractCollection<E> java.util.AbstractSet<E> java.util.EnumSet<E>
As you can see, EnumSet extends AbstractSet, which in turn extends AbstractCollection. AbstractCollection is a partial implementation of the Collection interface, while AbstractSet provides a skeletal implementation of the Set interface. EnumSet also implements the Cloneable and Serializable interfaces.
Since EnumSet is a specialized implementation of the Set interface, it inherits all the methods defined in the Set interface, such as add(), remove(), contains(), and size(). In addition, EnumSet provides a number of specialized methods for working with enum types, such as allOf(), complementOf(), range(), and of().
Overall, the EnumSet class hierarchy reflects its specialized nature as an implementation that is optimized for working with enum types.
EnumSet class declaration:
The EnumSet class is declared in the java.util
package in Java. The declaration of the EnumSet class looks like this:
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E> implements Cloneable, Serializable
Let’s break down this declaration:
- The class is declared as
public
which means that it can be accessed from any other class in the application. - The class is declared as
abstract
which means that it cannot be directly instantiated. Instead, it provides a set of methods and functionality that can be extended by other classes. - The class extends
AbstractSet<E>
which provides a skeletal implementation of the Set interface. - The type parameter
<E extends Enum<E>>
specifies that EnumSet can only be used with Enum types. This ensures that EnumSet can only contain elements of the specified Enum type. - The class implements the
Cloneable
andSerializable
interfaces, which means that it can be cloned and serialized respectively.
In summary, the declaration of the EnumSet class specifies that it is a specialized implementation of the Set interface that is optimized for working with enums, and provides a set of methods for performing set operations on enum values.
Methods of Java EnumSet class:
The Java EnumSet class provides a number of methods for working with enum types. Here are some of the key methods:
of(E e1, E e2, E... others)
: This method creates an EnumSet containing the specified enum values.
Example: EnumSet<Color> primaryColors = EnumSet.of(Color.RED, Color.BLUE, Color.GREEN);
allOf(Class<E> elementType)
: This method creates an EnumSet containing all the enum values for the specified enum type.
Example: EnumSet<Color> allColors = EnumSet.allOf(Color.class);
range(E from, E to)
: This method creates an EnumSet containing all the enum values between the specifiedfrom
andto
values, inclusive.
Example: EnumSet<Color> colors = EnumSet.range(Color.RED, Color.BLUE);
noneOf(Class<E> elementType)
: This method creates an empty EnumSet of the specified enum type.
Example: EnumSet<Color> noColors = EnumSet.noneOf(Color.class);
add(E e)
: This method adds the specified enum value to the EnumSet.
Example: colors.add(Color.GREEN);
remove(Object o)
: This method removes the specified object (an enum value) from the EnumSet.
Example: colors.remove(Color.BLUE);
contains(Object o)
: This method returnstrue
if the EnumSet contains the specified object (an enum value).
Example: boolean containsRed = colors.contains(Color.RED);
isEmpty()
: This method returnstrue
if the EnumSet is empty.
Example: boolean isEmpty = colors.isEmpty();
size()
: This method returns the number of elements in the EnumSet.
Example: int size = colors.size();
clear()
: This method removes all elements from the EnumSet.
Example: colors.clear();
These are some of the key methods provided by the Java EnumSet class for working with enum types. They provide a powerful and flexible way to manipulate sets of enum values in Java.
Java EnumSet Example:
Sure, here’s an example of using the Java EnumSet class:
import java.util.EnumSet; public class EnumSetExample { enum Weekday { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public static void main(String[] args) { EnumSet<Weekday> weekdays = EnumSet.of(Weekday.MONDAY, Weekday.TUESDAY, Weekday.WEDNESDAY, Weekday.THURSDAY, Weekday.FRIDAY); EnumSet<Weekday> weekendDays = EnumSet.of(Weekday.SATURDAY, Weekday.SUNDAY); System.out.println("Weekdays: " + weekdays); System.out.println("Weekend days: " + weekendDays); EnumSet<Weekday> allDays = EnumSet.allOf(Weekday.class); System.out.println("All days: " + allDays); EnumSet<Weekday> workingDays = EnumSet.complementOf(weekendDays); System.out.println("Working days: " + workingDays); System.out.println("Is Monday a working day? " + workingDays.contains(Weekday.MONDAY)); System.out.println("Is Saturday a working day? " + workingDays.contains(Weekday.SATURDAY)); weekdays.add(Weekday.FRIDAY); System.out.println("Weekdays after adding Friday: " + weekdays); weekdays.remove(Weekday.TUESDAY); System.out.println("Weekdays after removing Tuesday: " + weekdays); weekdays.clear(); System.out.println("Weekdays after clearing: " + weekdays); } }
This code defines an enum type Weekday
and creates two EnumSets: weekdays
containing Monday through Friday, and weekendDays
containing Saturday and Sunday. It then demonstrates several methods of the EnumSet class:
of()
is used to create the initial EnumSets.allOf()
creates an EnumSet containing all the enum values of a given type.complementOf()
creates an EnumSet containing all the values not in the given set.contains()
checks whether an EnumSet contains a given value.add()
andremove()
add and remove values from an EnumSet.clear()
removes all values from an EnumSet.
This example illustrates how EnumSet can be used to create, manipulate, and work with sets of enum values in Java.
Java EnumSet Example: allOf() and noneOf()
Sure, here’s an example that demonstrates the allOf()
and noneOf()
methods of the Java EnumSet class:
import java.util.EnumSet; public class EnumSetExample { enum Season { SPRING, SUMMER, FALL, WINTER } public static void main(String[] args) { EnumSet<Season> allSeasons = EnumSet.allOf(Season.class); System.out.println("All seasons: " + allSeasons); EnumSet<Season> noSeasons = EnumSet.noneOf(Season.class); System.out.println("No seasons: " + noSeasons); noSeasons.addAll(EnumSet.of(Season.SUMMER, Season.FALL)); System.out.println("No seasons after adding summer and fall: " + noSeasons); } }
This code defines an enum type Season
and creates two EnumSets: allSeasons
containing all four seasons, and noSeasons
containing no seasons initially. It then demonstrates two methods of the EnumSet class:
allOf()
creates an EnumSet containing all the enum values of a given type.noneOf()
creates an empty EnumSet of the given type.
Finally, the code adds SUMMER
and FALL
to the noSeasons
set using the addAll()
method, which adds all elements from the specified collection to the set.
This example illustrates how the allOf()
and noneOf()
methods can be used to quickly create EnumSets containing all or none of the enum values for a given type, respectively.