Java: 将 List<String> 转换为枚举列表
要将一个类的 'List
import java.util.ArrayList;
import java.util.List;
public class YourClass {
private List<String> stringList;
// 其他属性和方法...
public List<YourEnum> convertToEnumList() {
List<YourEnum> enumList = new ArrayList<>();
for (String value : stringList) {
YourEnum yourEnum = YourEnum.fromValue(value);
if (yourEnum != null) {
enumList.add(yourEnum);
}
}
return enumList;
}
}
在上述示例中,我们定义了一个 'convertToEnumList' 方法,该方法将 'stringList' 中的每个字符串值转换为相应的枚举值,并将其添加到新的 'List
请根据你的实际情况修改示例代码中的类名、属性字段名以及枚举类的相应方法。
原文地址: https://www.cveoy.top/t/topic/lhm 著作权归作者所有。请勿转载和采集!