In Java, an Enumeration (also known as an enum) is used to hold a set of named constants. Thus, an enum defines a new data type, each object of this data type can have one of the values specified within the enum (also known as enum constants or enumeration constants). In this article, we will be learning about the different ways in which you can iterate over an enum.
Defining an Enum
An enum can be defined via the enum keyword.
Sample Code
public enum Direction { NORTH, SOUTH, EAST, WEST; }
- This code specifies an enum called Direction.
- It defines the enum constants NORTH, SOUTH, EAST, and WEST.
- So, a Direction object can only have one of these values
Iterating over an enum using a for loop
Every enum has an in-built method called values. This returns an array that consists of the enumeration constants in the Enum. So, this method can be used to obtain an array of the enum constants. Once this array is obtained, any of the Java loops can be used to iterate over it.
Sample Code
Direction[] directionArray = Direction.values(); for(int i=0;i < directionArray.length;i++) { System.out.println(directionArray[i]); }
- Line 1 invokes the Direction.values method. This returns an array having values of type Direction which is assigned to directionArray
- Lines 2-4 specify a simple for loop that iterates over directionArray and prints each element from the array.
Output
NORTH
SOUTH
EAST
WEST
A for-each loop can also be used to iterate over the enumeration constants array.
Sample Code
Direction[] directionArray = Direction.values(); for(Direction d:directionArray) { System.out.println(d); }
In this case, Lines 2-4 specify a for-each loop to iterate over the array and print each element from the array. This code produces the same output as before. Similarly, the other loops like while/do-while can also be used to iterate over the enum array.
Iterating over an enum using the forEach method
All the Collection interfaces like List, Set, etc. have a method called forEach which can be used to internally iterate over the collection. The forEach method accepts as a parameter a Consumer instance. Consumer is an in-built functional interface that can be implemented via a lambda expression. The forEach method then applies the lambda expression to every element in the collection. Thus, in order to use the forEach method to iterate over an Enum, the enum needs to be converted into a collection. The enum can either be converted either into a List or a Set.
Converting Enum to List
As explained earlier, every enum has an in-built values method that returns an array corresponding to the enum constants. Java also has an in-built class called Arrays. The Arrays class has a static method called asList which accepts an array as a parameter and returns a List corresponding to the elements in the array. So, this method can be used to convert the enum constants array into a List.
Sample Code
List directionList = Arrays.asList(Direction.values()); directionList.forEach(d -> System.out.println(d));
- Line 1 invokes the Arrays.asList method on Direction.values. This returns a Direction List called directionList
- Line 2 invokes the forEach method on directionList. A lambda expression is specified that accepts a Direction object and prints it
Converting Enum to Set
Java has an in-built class called EnumSet. This has a static method called allOf which accepts as a parameter a class instance corresponding to the Enum type and returns a Set corresponding to an Enum.
Sample Code
Set directionSet = EnumSet.allOf(Direction.class); directionSet.forEach(d -> System.out.println(d));
- Line 1 invokes the EnumSet.allOf method. It specifies Direction.class as a parameter. This returns a Direction Set called directionSet
- Line 2 invokes the forEach method on directionSet. A lambda expression is specified that accepts a Direction object and prints it
Iterating over an enum using a Stream
Java supports the Stream interface that can be used to perform bulk operations on a group of objects in an array or collection. It has a forEach method that can be used to iterate over the stream. Like the Collection forEach method, the Stream forEach method accepts a lambda expression and applies the lambda expression to every element in the Stream. In order to use a Stream to iterate over an Enum, it needs to be converted into an array or a collection. A stream can then be obtained on the array/collection and the forEach method can be used to iterate over it.
Converting enum to a stream via an array
As explained earlier, the values method can be used to obtain an array corresponding to the enum. The Stream interface has a method called of which accepts an array as a parameter and returns a Stream corresponding to the array.
Sample Code
Direction[] directionArray = Direction.values(); Stream.of(directionArray).forEach(d -> System.out.println(d));
- Line 1 invokes the Direction.values method. This returns a Direction array called directionArray
- Line 2 uses the Stream.of method with the directionArray. This returns a Stream corresponding to directionArray. The forEach method is then invoked on the stream. A lambda expression is passed to the forEach method that prints each element in the stream
The in-built Arrays class also has a method called stream which can also be used to obtain a stream from an array.
Sample Code
Direction[] directionArray = Direction.values(); Arrays.stream(directionArray).forEach(d -> System.out.println(d));
In this case, Line 2 uses the Arrays.stream method using the directionArray. This returns a Stream corresponding to directionArray. The forEach method is then invoked as before.
Converting Enum to Collection
As explained earlier, an Enum can be converted either to a List or a Set via the Arrays.asList method or the EnumSet.allOf method. Once a List/Set is obtained, a stream can be obtained on the List/Set via the stream method and the forEach method can be used to iterate over the Stream
Sample Code
List directionList = Arrays.asList(Direction.values()); directionList.stream().forEach(d -> System.out.println(d));
- Line 1 invokes the Arrays.asList method on Direction.values. This returns a Direction List called directionList.
- Line 2 invokes the stream method on directionsList. This returns a stream corresponding to directionList. The forEach method is then invoked on the stream.
- We can similarly create a Set using the Direction enum, obtain a Stream on it and invoke the forEach method on the stream.
Conclusion
So, in this article, we learned about enumerations and the different ways in which you can iterate over an enumeration. We first learned how to iterate over an enum using simple loops, using the forEach method, and using a Stream.