Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In Java, an Enum is a datatype that helps us assign a predefined set of constants to a variable.

In this quick tutorial, we’ll learn different ways that we can iterate over an Enum in Java.

2. Iterating Over Enum Values

Let’s first define an Enum, so we can create some simple code examples:

public enum DaysOfWeekEnum {
    SUNDAY,
    MONDAY,
    TUESDAY, 
    WEDNESDAY, 
    THURSDAY, 
    FRIDAY, 
    SATURDAY
}

Enums don’t have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.

2.1. Iterate Using for Loop

First, we can simply use the old-school for loop:

for (DaysOfWeekEnum day : DaysOfWeekEnum.values()) { 
    System.out.println(day); 
}

2.2. Iterate Using Stream

We can also use java.util.Stream to perform operations on the Enum values.

To create a Stream, we have two options. The first is using Stream.of:

Stream.of(DaysOfWeekEnum.values());

The second is using Arrays.stream:

Arrays.stream(DaysOfWeekEnum.values());

Let’s extend the DaysOfWeekEnum class to create an example using Stream:

public enum DaysOfWeekEnum {
    
    SUNDAY("off"), 
    MONDAY("working"), 
    TUESDAY("working"), 
    WEDNESDAY("working"), 
    THURSDAY("working"), 
    FRIDAY("working"), 
    SATURDAY("off");

    private String typeOfDay;

    DaysOfWeekEnum(String typeOfDay) {
        this.typeOfDay = typeOfDay;
    }
	
    // standard getters and setters 

    public static Stream<DaysOfWeekEnum> stream() {
        return Stream.of(DaysOfWeekEnum.values()); 
    }
}

Now we’ll write an example in order to print the non-working days:

public class EnumStreamExample {

    public static void main() {
        DaysOfWeekEnum.stream()
        .filter(d -> d.getTypeOfDay().equals("off"))
        .forEach(System.out::println);
    }
}

The output we get when we run this is:

SUNDAY
SATURDAY

2.3. Iterate Using forEach()

The forEach() method was added to the Iterable interface in Java 8. So all the java collection classes have implementations of a forEach() method. In order to use these with an Enum, we first need to convert the Enum to a suitable collection. We can use Arrays.asList() to generate an ArrayList, which we can then loop around using the forEach() method:

Arrays.asList(DaysOfWeekEnum.values())
  .forEach(day -> System.out.println(day));

2.4. Iterate Using EnumSet

EnumSet is a specialized set implementation that we can use with Enum types:

EnumSet.allOf(DaysOfWeekEnum.class)
  .forEach(day -> System.out.println(day));

2.5. Using an ArrayList of Enum Values

We can also add the values of an Enum to a List. This allows us to manipulate the List like any other:

List<DaysOfWeekEnum> days = new ArrayList<>();
days.add(DaysOfWeekEnum.FRIDAY);
days.add(DaysOfWeekEnum.SATURDAY);
days.add(DaysOfWeekEnum.SUNDAY);
for (DaysOfWeekEnum day : days) {
    System.out.println(day);
}
days.remove(DaysOfWeekEnum.SATURDAY);
if (!days.contains(DaysOfWeekEnum.SATURDAY)) {
    System.out.println("Saturday is no longer in the list");
}
for (DaysOfWeekEnum day : days) {
    System.out.println(day);
}

We can also create an ArrayList by using Arrays.asList().

However, as the ArrayList is backed by the Enum values array, it’ll be immutable, so we can’t add or remove items from the List. The remove in the following code would fail with an UnsupportedOperationException:

List<DaysOfWeekEnum> days = Arrays.asList(DaysOfWeekEnum.values());
days.remove(0);

3. Conclusion

In this article, we discussed various ways to iterate over an Enum using forEach, Stream, and the for loop in Java.

If we need to perform any parallel operations, Stream is a good option. Otherwise, there’s no restriction on which method to use.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW