Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Java introduced enum in version 1.5. Defining constants as enum makes the code more readable. Further, it allows compile-time checking.

In this quick tutorial, let’s explore how to get a List with all instances of an enum type.

2. Introduction to the Problem

As usual, we’ll understand the problem through an example.

First, let’s  create an enum type MagicNumber:

enum MagicNumber {
    ONE, TWO, THREE, FOUR, FIVE
}

Then, our goal is to obtain a List filled with all instances of the MagicNumber enum:

List<MagicNumber> EXPECTED_LIST = Arrays.asList(ONE, TWO, THREE, FOUR, FIVE);

Here, we’ve used the Arrays.asList() method to initialize the list from an array.

Later on, we’ll explore a few different ways to get the expected result. Finally, for simplicity, we’ll use unit test assertions to verify if each method gives the desired result.

So next, let’s see them in action.

3. Using the EnumType.values() Method

When we prepared EXPECTED_LIST, we initialized it from an array. Therefore, if we can get all instances from an enum in an array, we can build the list and solve the problem.

Each enum type provides the standard values() method to return all instances in an array. So next, let’s build up a list from MagicNumber.values():

List<MagicNumber> result = Arrays.asList(MagicNumber.values());
assertEquals(EXPECTED_LIST, result);

If we run the test, it passes. So, we’ve got the expected list.

4. Using the EnumType.class.getEnumConstants() Method

We’ve seen using the enum type’s values() to get all enum instances in an array. This is a standard and straightforward method. However, we need to know exactly the enum type’s name and hardcode it in the code, for example, MagicNumber.values(). In other words, in this way, we can’t build a utility method to work for all enum types.

Since Java 1.5, the Class object provides the getEnumConstants() method to get all enum instances from an enum Class object. Therefore, we can let getEnumConstants() provide the enum instances:

List<MagicNumber> result = Arrays.asList(MagicNumber.class.getEnumConstants());
assertEquals(EXPECTED_LIST, result);

As the test above shows, we’ve used MagicNumber.class.getEnumConstants() to provide the enum instance array. Further, it’s easy to build a utility method to work for all enum types:

static <T> List<T> enumValuesInList(Class<T> enumCls) {
    T[] arr = enumCls.getEnumConstants();
    return arr == null ? Collections.emptyList() : Arrays.asList(arr);
}

It’s worth mentioning that if the Class object is not an enum type, the getEnumConstants() method returns null. As we can see, we return an empty List in this case.

Next, let’s create a test to verify enumValuesInList():

List<MagicNumber> result1 = enumValuesInList(MagicNumber.class);
assertEquals(EXPECTED_LIST, result1);
                                                                
List<Integer> result2 = enumValuesInList(Integer.class);
assertTrue(result2.isEmpty());

The test passes if we give it a run. As we can see, if the class object is not in the enum type, we have an empty List.

5. Using the EnumSet.allOf() Method

Since version 1.5, Java has introduced a particular Set to work with enum classes: EnumSet. Further, EnumSet has the allOf() method to load all instances of a given enum type.

Therefore, we can use the ArrayList() constructor and the filled EnumSet to construct a List object. So next, let’s see how it works through a test:

List<MagicNumber> result = new ArrayList<>(EnumSet.allOf(MagicNumber.class));
assertEquals(EXPECTED_LIST, result);

It’s worth mentioning that calling the allOf() method stores the enum‘s instances in the natural order.

6. Conclusion

In this article, we’ve learned three approaches to get a List object that contains all instances of an enum.

As usual, all code snippets presented here are available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.