如何获取Java中的所有枚举值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2269803/
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-08-13 05:29:45  来源:igfitidea点击:

How to get all enum values in Java?

javaenums

提问by Roman

I came across this problem that I without knowing the actual enumtype I need to iterate its possible values.

我遇到了这个问题,我不知道enum迭代其可能值所需的实际类型。

if (value instanceof Enum){
   Enum enumValue = (Enum)value;
}

Any ideas how to extract from enumValue its possible values ?

任何想法如何从 enumValue 中提取其可能的值?

采纳答案by ColinD

Object[] possibleValues = enumValue.getDeclaringClass().getEnumConstants();

回答by RodeoClown

Enums are just like Classes in that they are typed. Your current code just checks if it is an Enum without specifying what type of Enum it is a part of.

Enums 就像Classes一样,因为它们是类型化的。您当前的代码只是检查它是否是 Enum 而不指定它属于什么类型的 Enum 。

Because you haven't specified the type of the enum, you will have to use reflection to find out what the list of enum values is.

因为您还没有指定枚举的类型,所以您必须使用反射来找出枚举值的列表。

You can do it like so:

你可以这样做:

enumValue.getDeclaringClass().getEnumConstants() 

This will return an array of Enum objects, with each being one of the available options.

这将返回一个 Enum 对象数组,每个对象都是可用选项之一。

回答by deleted

... or MyEnum.values() ? Or am I missing something?

...或 MyEnum.values() ?或者我错过了什么?

回答by someone

YourEnumClass[] yourEnums = YourEnumClass.class.getEnumConstants();

Or

或者

YourEnumClass[] yourEnums = YourEnumClass.values();

回答by Rajesh Dixit

values method of enum

枚举的值方法

enum.values() method which returns all enum instances.

enum.values() 方法返回所有枚举实例。

  public class EnumTest {
        private enum Currency {
        PENNY("1 rs"), NICKLE("5 rs"), DIME("10 rs"), QUARTER("25 rs");
        private String value;
        private Currency(String brand) {
              this.value = brand;
        }

        @Override
        public String toString() {
              return value;
        }
  }

  public static void main(String args[]) {

        Currency[] currencies = Currency.values();

        // enum name using name method
        // enum to String using toString() method
        for (Currency currency : currencies) {
              System.out.printf("[ Currency : %s,
                         Value : %s ]%n",currency.name(),currency);
        }
  }
}

http://javaexplorer03.blogspot.in/2015/10/name-and-values-method-of-enum.html

http://javaexplorer03.blogspot.in/2015/10/name-and-values-method-of-enum.html

回答by Ashwani Sharma

Here, Role is an enum which contains the following values [ADMIN, USER, OTHER].

此处,Role 是一个枚举,其中包含以下值 [ADMIN、USER、OTHER]。

List<Role> roleList = Arrays.asList(Role.values());
roleList.forEach(role -> {
    System.out.println(role);
    });

回答by David Lilljegren

One can also use the java.util.EnumSet like this

也可以像这样使用 java.util.EnumSet

@Test
void test(){
    Enum aEnum =DayOfWeek.MONDAY;
    printAll(aEnum);
}

void printAll(Enum value){
    Set allValues = EnumSet.allOf(value.getClass());
    System.out.println(allValues);
}