2

I'm having an Enum array. Now I want to convert it to a String array which contains the names of the enums returned by the method Enum#name(). Here's what I tried so far (The enum is called "Column".):

String[] stringArray = Arrays.asList(Column.values()).toArray(String[]::new);

I'm alway getting an ArrayStoreException. What can I do?

dennisp
  • 93
  • 1
  • 7

1 Answers1

5

You need to stream the enum in order to first map the enum to String before creating the array:

String[] arrStr = Arrays.stream(FooEnum.values()) // create stream of enum values
        .map(e -> e.toString())  // convert enum stream to String stream
        .toArray(String[]::new); // convert stream to an array
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373