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;
    }
}
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • 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. – the8472 Oct 13 '15 at 10:53

1 Answers1

16

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

Eran
  • 387,369
  • 54
  • 702
  • 768