response.setPeopleHealth(new ArrayList<String>() {{
for(HealthEnum e :HealthEnum.values()) {
add(e.getValue());
}
}})
索纳林特对此发出警告。
我试图转换为单调,但它不适用于 for,因为它不能像 arraylist 那样添加。
我可以使用 lambda 或流吗?
对于初学者来说,停止使用著名的反模式new ArrayList<String>() {{...
,而不是首先考虑你的问题,你有一个数组 - 需要一个List
,因此:
List<String> list = Arrays.stream(HealthEnum.values())
.map(HealthEnum::getValue)
.collect(Collectors.toCollection(ArrayList::new));
并将此ArrayList
传递到您想要的位置,例如:
response.setPeopleHealth(list);