How to get all enum values in Java?

JavaEnums

Java Problem Overview


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

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

Any ideas how to extract from enumValue its possible values ?

Java Solutions


Solution 1 - Java

Call Class#getEnumConstants to get the enum’s elements (or get null if not an enum class).

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

Solution 2 - Java

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

Or

YourEnumClass[] yourEnums = YourEnumClass.values();

Solution 3 - Java

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.

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.

Solution 4 - Java

values method of enum

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

  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

Solution 5 - Java

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

Solution 6 - Java

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

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

Solution 7 - Java

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

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

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

Solution 8 - Java

Any one who is trying to fetch all the values as list can simply do this.

Arrays.asList(YouEnumClass.values())

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
QuestionRomanView Question on Stackoverflow
Solution 1 - JavaColinDView Answer on Stackoverflow
Solution 2 - JavasomeoneView Answer on Stackoverflow
Solution 3 - JavaRodeoClownView Answer on Stackoverflow
Solution 4 - JavaRajesh DixitView Answer on Stackoverflow
Solution 5 - JavadeletedView Answer on Stackoverflow
Solution 6 - JavaAshwani SharmaView Answer on Stackoverflow
Solution 7 - JavaDavid LilljegrenView Answer on Stackoverflow
Solution 8 - JavaNagarjun NageshView Answer on Stackoverflow