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 |
The binary operator XOR (Exclusive OR) is a basic operation in computer programming, including Java. It is an arithmetic operator that performs a bitwise exclusive OR operation on two operands of the same data type and returns a new value based on the outcome. In this...
4 min read
Double brace initialization is a technique used in Java to initialize an instance of a class and provide initial values for its fields in a concise and convenient manner. It involves the use of nested curly braces within the instantiation code block. Although the approach can...
4 min read
When a block is decorated or associated with the word static, it is called a static block. Static Block is known as the static clause. A static block can be used for the static initialization of a class. The code that is written inside the static...
4 min read
Image processing is a fascinating field within computer science, encompassing a wide range of operations for analyzing and manipulating images. One of the most basic yet intriguing tasks in image processing is generating an image with randomly colored pixels. This task can serve as an...
4 min read
Java, a powerful and versatile programming language, provides numerous tools and libraries that can assist businesses in addressing complex challenges. One such challenge involves solving business board problems, which require efficient algorithms and data structures to optimize decision-making processes. In this section, we will explore how...
6 min read
The java.text.RuleBasedCollator class has an equals() function. A collator class is used to determine whether or not the two strings that are supplied are identical. Syntax: public boolean equals(String source, String target) Parameter: In order to compare two strings, this method requires two strings. Return...
2 min read
Through Java collections, developers gain a powerful toolset that helps them efficiently maintain and operate object groups in their Java programming environment. Java collections function as non-generic and generic collections, respectively. The addition of generics in Java 5 provided collections with a major advancement that improved both...
5 min read
Handling key-value-based data is a common requirement in various Java applications. Often, data arrives as Strings or String arrays, and it becomes essential to convert them into Maps for efficient processing. In the same context, Map provide an easy way to access and manipulate data with...
5 min read
Problem Statement Given a binary string we need to find the maximum difference of 0s and 1s inside the given binary string. Here, you treat 0 as +1 and 1 as -1, and then seek the maximum value of contiguous subarray. This maximum sum of a subarray...
4 min read
Java is a popular programming language that is widely used for developing various types of applications. One of the common tasks in many applications is to get a date from a calendar. In this section, we will explore how to get a date from a calendar...
3 min read
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