Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, 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.

Generic bottom

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

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments