The EnumSet<E>
class is defined as:
public abstract class EnumSet<E extends Enum<E>>
extends AbstractSet<E>
implements Cloneable, Serializable
in JCF.
Also, most of the methods that the class itself implements are static. Lastly, the class does not seem to implement the add()
,iterator()
,remove()
,size()
,contains()
or isEmpty()
methods and just inherits them from AbstractSet which doesn't implement them.
I have two questions:
EnumSet
objects are instantiated and used?add()
method for EnumSet
objects?EnumSet of() Method in Java method in Java is used to create an enum set initially containing the specified elements in the parameters.
An EnumSet is a specialized Set collection to work with enum classes. It implements the Set interface and extends from AbstractSet: Even though AbstractSet and AbstractCollection provide implementations for almost all the methods of the Set and Collection interfaces, EnumSet overrides most of them.
EnumSet. noneOf(Class elementType ) method in Java is used to create a null set of the type elementType.
Use EnumSet. noneOf(Class) to create an empty EnumSet.
Most of the static methods you see are factory methods (of()
, complementOf()
, allOf()
, etc.).
These methods return an instance of EnumSet. The actual type of the EnumSet created and returned by these methods is a subclass of EnumSet (RegularEnumSet
, JumboEnumSet
), which are not part of the public API, but implement all the required methods. All you need to know is that they implement EnumSet.
EnumSet is an abstract class itself, so it doesn't have to implement any abstract methods it inherits. It passes that responsibility on to its non-abstract subclasses.
You can call the unimplemented methods because you're actually calling them on an instance of a subclass of EnumSet. (As EnumSet is abstract, it cannot be directly instantiated itself.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With