How to create empty EnumSet?

David162795 picture David162795 · Dec 13, 2016 · Viewed 18.6k times · Source

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.

Answer

Jesper picture Jesper · Dec 13, 2016

Use the method EnumSet.noneOf:

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