Java 8 way to work with an enum [closed]

I’d go for EnumSet. Because forEach() is also defined on Iterable, you can avoid creating the stream altogether:

EnumSet.allOf(Letter.class).forEach(x -> foo(x));

Or with a method reference:

EnumSet.allOf(Letter.class).forEach(this::foo);

Still, the oldschool for-loop feels a bit simpler:

for (Letter x : Letter.values()) {
    foo(x);
}

Leave a Comment