Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW