Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create empty EnumSet?

Tags:

java

enumset

I am struggling with EnumSet as it surprisingly doesn't have a simple constructor and its methods doesn't like null values.

What I came up with: EnumSet<MyClass> x = EnumSet.copyOf(Collections.<MyClass>emptySet());

Which somewhat works but doesn't seem right to me.

like image 775
David162795 Avatar asked Dec 13 '16 09:12

David162795


People also ask

What is EnumSet in Java?

An enumSet is a Java Collections member, it is not synchronized. An enumSet is a high performing set implementation that works faster than the HashSet. All the elements in an enumSet must be of a single enumeration type that is specified when the set is created.

Is EnumSet immutable?

Yes EnumSet is modifiable. If you want to create an immutable EnumSet then go for immutableEnumSet. Returns an immutable set instance containing the given enum elements. Internally, the returned set will be backed by an EnumSet .

How do I create an empty enum set?

Use EnumSet. noneOf(Class) to create an empty EnumSet.

What is the use of EnumSet?

The EnumSet is one of the specialized implementations of the Set interface for use with the enumeration type. A few important features of EnumSet are as follows: It extends AbstractSet class and implements Set Interface in Java. EnumSet class is a member of the Java Collections Framework & is not synchronized.


1 Answers

Use the method EnumSet.noneOf:

EnumSet<MyClass> x = EnumSet.noneOf(MyClass.class); 
like image 129
Jesper Avatar answered Sep 22 '22 23:09

Jesper