java 如何创建空的 EnumSet?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41117898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:41:18  来源:igfitidea点击:

How to create empty EnumSet?

javaenumset

提问by David162795

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

我正在为 EnumSet 苦苦挣扎,因为它令人惊讶地没有简单的构造函数,而且它的方法不喜欢空值。

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

我想出了什么: EnumSet<MyClass> x = EnumSet.copyOf(Collections.<MyClass>emptySet());

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

这有点有效,但对我来说似乎不对。

回答by Jesper

Use the method EnumSet.noneOf:

使用方法EnumSet.noneOf

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

回答by Fabian Barney

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

使用EnumSet.noneOf(Class)创建一个空的EnumSet。