繁体   English   中英

如何将枚举列表转换为字符串数组?

[英]How to convert list of enums into array of strings?

我有一系列枚举,如下所示:

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

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

如何将我的countryList转换为String[]

我以这种方式尝试 eq :

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

但它返回给我ArrayStoreException

谢谢你的帮助!

谢谢你的帮助!

它应该这样工作:

 String[] strings = countryList.stream()
                    .map(Enum::toString)
                    .toArray(String[]::new);

尝试这个:

CountryEnum[] arr = countryList.stream().toArray(CountryEnum[] ::new);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM