Java Program to Iterate over enum
What is enum in Java ?
In Java, an enum (short for “enumeration”) is a special type that allows you to define a set of named constants. An enum type is essentially a fixed set of values that are defined at compile time, and cannot be modified at runtime.
One of the main advantages of using enum types is that they provide a more type-safe and expressive way to represent a fixed set of values in your code. They also provide a more readable way to write switch statements, as the case labels can be the enum constants themselves.
Declaring an enum type is similar to declaring a class:
Syntax :
(parameter1, parameter2, ...) -> { statements }
Here’s the algorithm to iterate over an enum in Java:
- Declare the enum type.
- Use the values() method to get an array of the constants in the enum.
- Use a for-each loop to iterate over the array and process each constant.
- (Optional) Use the ordinal() method to get the index of each constant in the enum.
- (Optional) Use the compareTo() method to compare the constants if a specific order is needed.
Here’s a pseudo-code of the algorithm :
// Declare the enum type
enum MyEnum {
VALUE1,
VALUE2,
VALUE3
}
// Get an array of the constants in the enum
MyEnum[] values = MyEnum.values()
// Use a for-each loop to iterate over the array and process each constant
for (MyEnum value : values) {
// Process the current constant
// ...
}
// (Optional) Use the ordinal() method to get the index of each constant in the enum
for (MyEnum value : values) {
int index = value.ordinal()
// Process the current constant and its index
// ...
}
// (Optional) Use the compareTo() method to compare the constants if a specific order is needed
Arrays.sort(values)
for (MyEnum value : values) {
// Process the current constant in sorted order
// ...
}
Example 1 :
import java.util.*;
enum DayOfWeek {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public class Main {
public static void main(String[] args) {
// Get an array of the constants in the enum
DayOfWeek[] days = DayOfWeek.values();
// Use a for-each loop to iterate over the array and process each constant
for (DayOfWeek day : days) {
System.out.println(day);
}
// Use the ordinal() method to get the index of each constant in the enum
for (DayOfWeek day : days) {
int index = day.ordinal();
System.out.println(day + " has index " + index);
}
// Use the compareTo() method to compare the constants if a specific order is needed
Arrays.sort(days);
for (DayOfWeek day : days) {
System.out.println(day);
}
}
}
Output :
SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY has index 0 MONDAY has index 1 TUESDAY has index 2 WEDNESDAY has index 3 THURSDAY has index 4 FRIDAY has index 5 SATURDAY has index 6 SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
Example 2 :
import java.util.*;
enum Size {
SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
private String abbreviation;
private Size(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getAbbreviation() {
return abbreviation;
}
}
public class Main {
public static void main(String[] args) {
// Get an array of the constants in the enum
Size[] sizes = Size.values();
// Use a for-each loop to iterate over the array and process each constant
for (Size size : sizes) {
System.out.println(size + " (" + size.getAbbreviation() + ")");
}
// Use the ordinal() method to get the index of each constant in the enum
for (Size size : sizes) {
int index = size.ordinal();
System.out.println(size + " has index " + index);
}
// Use the compareTo() method to compare the constants if a specific order is needed
Arrays.sort(sizes);
for (Size size : sizes) {
System.out.println(size + " (" + size.getAbbreviation() + ")");
}
}
}
Output :
SMALL (S) MEDIUM (M) LARGE (L) EXTRA_LARGE (XL) SMALL has index 0 MEDIUM has index 1 LARGE has index 2 EXTRA_LARGE has index 3 SMALL (S) MEDIUM (M) LARGE (L) EXTRA_LARGE (XL)
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment