[英]How to convert list of enums into array of strings?
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[]
?如何将我的
countryList
转换为String[]
?
I try eq in this way:我以这种方式尝试 eq :
String[] countries = countryList.toArray(String::new);
but it return me ArrayStoreException
.但它返回给我
ArrayStoreException
。
Thanks for any help!谢谢你的帮助!
Thanks for any help!谢谢你的帮助!
It should work so:它应该这样工作:
String[] strings = countryList.stream()
.map(Enum::toString)
.toArray(String[]::new);
Try this:尝试这个:
CountryEnum[] arr = countryList.stream().toArray(CountryEnum[] ::new);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.