Enum array to string array

dennisp :

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?

Hovercraft Full Of Eels :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=129395&siteId=1