How to convert list of enums into array of strings?

BokuNoHeeeero

I've had an array of enums like below:

enum CountryEnum {
   MADAGASCAR,
   LESOTHO,
   BAHAMAS,
   TURKEY,
   UAE
}

List<CountryEnum> countryList = new ArrayList<>();
countryList.add(CountryEnum.MADAGASCAR);
countryList.add(CountryEnum.BAHAMAS);

How to convert my countryList into String[]?

I've tried this way:

String[] countries = countryList.toArray(String::new);

but it returned me ArrayStoreException.

Georgy Lvov

It should work so:

 String[] strings = countryList.stream() // create Stream of enums from your list
                    .map(Enum::toString) // map each enum to desired string (here we take simple toString() for each enum)
                    .toArray(String[]::new); // collect it into string array

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert array of enums to array of its representative strings

How to convert a list of strings into a numeric numpy array?

How to convert an array of integers to array of enums?

Convert array of Strings to Array list

How to convert an entire list of strings to a normal array or list?

How to convert Array of Strings with space into single Array list in Javascript?

Convert array of strings to List<string>

Convert array of Strings to list of Integers?

How to convert strings in a Pandas Dataframe to a list or an array of characters?

How to convert Python list of strings into C array of wchar_t?

How do I convert an array of strings into append-able list?

How to convert different strings in python list to array in bash script

How can I convert a list of strings to an array of float numbers?

How to convert a list of Strings to a LinkedHashMap?

How to convert list of strings into floats?

How to convert array of strings in Typescript

StringBuilder to Array of Strings how to convert

How to convert JSON to array of strings

How to convert an array of strings into a number?

How to convert typescript types of strings to array of strings?

ValueError: could not convert string to float---how to convert a list of lists of strings into a numpy array type float?

Convert strings within an array to list in Python

Java convert a string to list of enums

Convert Enums to List of given type

how to convert a list of strings into a list of variables?

How to convert a list of strings into a list of objects?

how to convert list of strings to list of guids

Ansible: How to convert list of strings to a list of dictionaries

How to convert a list of strings to list of dictionaries in python?