How to create empty EnumSet?

JavaEnumset

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

Use the method EnumSet.noneOf:

EnumSet<MyClass> x = EnumSet.noneOf(MyClass.class);

Solution 2 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid162795View Question on Stackoverflow
Solution 1 - JavaJesperView Answer on Stackoverflow
Solution 2 - JavaFabian BarneyView Answer on Stackoverflow