Filling a List with all Enum Values ​​in Java

1. Overview

introduced java enum In version 1.5. defining constants enum Makes the code more readable. Furthermore, it allows compile-time checking.

In this quick tutorial, let’s see how to get list with all instances of a enum type.

2. Introduction to the problem

As always, we’ll understand this problem through an example.

First, let’s make a enum type magic number,

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

Then, our goal is to get a list is full of examples of magicnumber enum,

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

Here, we have used Arrays.asList() Method to initialize a list from an array.

Later, we’ll explore a few different ways to achieve the expected results. Finally, for the sake of simplicity, we’ll use unit test assertions to verify whether each method returns the desired result.

So next, let’s see them in action.

3. Using EnumType.values() method

when we’re ready EXPECTED_LIST We initialize it with an array. So, if we can get all instances from one enum In an array, we can list and solve the problem.

each enum provides type standard Value () Method to return all instances in an array, So next, let’s make a list MagicNumber.value(),

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

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

4. Using EnumType.class.getEnumConstants() method

we have seen using enum kind of Value () to get all enum Example in an array. This is a standard and straightforward method. However, we need to know exactly enum type name and hardcode it in the code, for example, MagicNumber.value(), In other words, in this way, we can’t make a useful method work for everyone. enum type.

Since Java 1.5, Class item provides getEnumConstants() how to get everything enum example from one enum class Thing, so we can give getEnumConstants() render enum Example:

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

As the test above shows, we used MagicNumber.class.getEnumConstants() To provide enum example array. In addition, it is easier to build a utility method that works for everyone. enum type:

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

It is worth mentioning that If Class object is not a enum write getEnumConstants() method returns Zero, As we can see, we return an empty list in this matter.

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());

If we give it a run then the test passes. As we can see, if the class object does not contain enum type, we have an empty list,

5. Using EnumSet.allOf() method

Since version 1.5, Java has introduced a special group to work with enum Classes: EnumSet, ahead, EnumSet Is all of () method to load all instances of a given enum type,

Therefore, we can use array list() constructor and loaded EnumSet to build a list Thing. 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 is worth saying is calling all of () stores the method enumExamples of in natural order.

6. conclusion

In this article, we have learned three ways to get a list object that contains all instances of a enum.

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

       

Leave a Comment