3

Why I cannot invoke stream() on the array type Enum?

DummyEnum[] array = DummyEnum.values();
array.stream(); // Compile Error

ENUM:

public enum DummyEnum {
    Hello("Hello"), Welcome("Welcome");
    private String greeting;

    private DummyEnum(final String greeting) {
        this.greeting = greeting;
    }

    public String getValue() {
        return greeting;
    }
}
1
  • 3
    note that DummyEnum.values() creates a new array on each call. That's usually not a problem, but if you're doing it in extremely hot code you might want to cache a single instance in a static field instead. Commented Oct 13, 2015 at 10:53

1 Answer 1

16

Use Stream.of(array). Arrays don't have a stream() method.

Sign up to request clarification or add additional context in comments.

1 Comment

Or EnumSet.allOf(DummyEnum.class).stream()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.