An enum in Java is a special data type used to define collections of constants. Sometimes, you may need to iterate through all the constants defined in an enum for tasks like displaying options or performing specific actions based on enum values.
This tutorial from The Coding College will guide you through the process of looping through an enum in Java with examples.
What is an Enum in Java?
Enums are used to represent a fixed set of constants. For example, days of the week, months of the year, or directions (NORTH, SOUTH, EAST, WEST). Enums enhance code readability and prevent invalid values.
Example Enum
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
Method 1: Using a For-Each Loop
The simplest way to loop through an enum is by using a for-each
loop. The values()
method returns an array of all the constants in the enum.
Example Code
public class Main {
public static void main(String[] args) {
for (Day day : Day.values()) {
System.out.println("Day: " + day);
}
}
}
Output:
Day: MONDAY
Day: TUESDAY
Day: WEDNESDAY
Day: THURSDAY
Day: FRIDAY
Day: SATURDAY
Day: SUNDAY
Method 2: Using a For Loop
If you prefer a traditional for
loop, you can use the values()
method along with the array indices.
Example Code
public class Main {
public static void main(String[] args) {
Day[] days = Day.values();
for (int i = 0; i < days.length; i++) {
System.out.println("Day: " + days[i]);
}
}
}
Output:
Day: MONDAY
Day: TUESDAY
Day: WEDNESDAY
Day: THURSDAY
Day: FRIDAY
Day: SATURDAY
Day: SUNDAY
Method 3: Using Streams (Java 8+)
Streams provide a modern and functional way to loop through an enum.
Example Code
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Arrays.stream(Day.values()).forEach(day -> System.out.println("Day: " + day));
}
}
Output:
Day: MONDAY
Day: TUESDAY
Day: WEDNESDAY
Day: THURSDAY
Day: FRIDAY
Day: SATURDAY
Day: SUNDAY
Accessing Enum Properties
Enums can also have fields, methods, and constructors. Here’s an example of looping through an enum with properties.
Example Enum with Properties
public enum Day {
MONDAY("Start of the workweek"),
FRIDAY("End of the workweek"),
SUNDAY("Weekend!");
private String description;
Day(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
Loop Through Enum with Properties
public class Main {
public static void main(String[] args) {
for (Day day : Day.values()) {
System.out.println("Day: " + day + " - " + day.getDescription());
}
}
}
Output:
Day: MONDAY - Start of the workweek
Day: FRIDAY - End of the workweek
Day: SUNDAY - Weekend!
Best Practices
- Use Enums for Fixed Constants: Enums are best for predefined, constant values such as days, months, or user roles.
- Add Meaningful Methods: Use custom properties and methods in enums to improve readability and functionality.
- Avoid Overuse: Don’t use enums for mutable or large data collections.
Conclusion
Iterating through an enum is straightforward and can be done using various approaches like for-each
loops, traditional loops, or Java 8+ streams. You can also enhance enums with custom properties and methods for more practical use cases.
Visit The Coding College for more Java tutorials and elevate your coding expertise!