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
.
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.
Comments