Iterating Over Enum Values in Java26 Mar 2025 | 3 min read Enumerations or enum, is a distinct Java data type. Enums are a unified set of connected constants, such as days of the week or directions. Enums are used to provide type safety that reduces error rates. It is one of its main benefit. Working with enums frequently involves iterating over their values. In this section, we will discuss various ways for iterating enum values in Java, including code samples and explanations of each strategy. Day.java In this example, Day is an enum type with seven values as seven days of the week. Iterating Over Enum ValuesThere are several ways to iterate over the values of an enum in Java:
1. Using the values() MethodThe values() method is implicitly declared by the compiler for every enum in Java. This method returns an array containing all the constants of the enum in the order they are declared. ExampleOutput: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY In the above code, we call the values() method on the Day enum to get an array of all its constants, which we then iterate over using a for-each loop. 2. Using a for-each LoopAs the values() method returns an array, we can use a for-each loop to iterate over enum values without explicitly storing them in an array. ExampleOutput: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY This approach employs a for-each loop with the values() function. Java automatically provides the values() method for all enums, which returns an array of all the constants declared in the enum. In the supplied code, for (Day day: Day.values()) generates a loop that iterates over each Day enum element. 3. Iteration using EnumSetA specific Set implementation for usage with enum types is called EnumSet. It is a very concise and effective method of working with enum constants. You may iterate over a set of enum values that you generate using EnumSet. File Name: EnumIterationExample.java Output: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY In this example, an EnumSet containing every Day enum constant is created using EnumSet.allOf(Day.class). After that, a for-each loop is used to go over this set. 4. Utilising Java StreamsWe can utilize Java streams to iterate over enum values because Java 8 brought about the Streams API. Java Streams offer a practical method for handling elemental sequences, improving the clarity of the code. File Name: EnumIterationExample.java Output: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY In this example, the enum values are created as a stream using Arrays.stream(Day.values()), and each value is printed using forEach(System.out::println). With the help of the robust Streams API, more sophisticated processing may be carried out by combining it with additional functions like filtering, mapping, etc. ConclusionIn Java, iterating over enum values is a frequent activity that has several benefits when completed in different ways. The values() function offers a simple technique to obtain an array of enum constants. Iterating over enums in a condensed and legible manner is possible with a for-each loop. For carrying out set operations, EnumSet offers a condensed and effective set implementation designed specifically for enums. Java 8 introduces the Streams API, which offers a useful and expressive method for processing and iterating enum data. Next TopicShunting yard algorithm |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India