要将一个类的 'List' 属性字段转换为相应枚举类的值,你可以使用循环遍历的方式逐个转换每个字符串并添加到新的 'List' 中。下面是一个示例代码,假设你的类名为 'YourClass',包含一个 'List' 属性字段 'stringList',以及对应的枚举类 'YourEnum':

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' 中。我们假设 'YourEnum' 类中提供了一个静态方法 'fromValue',用于根据字符串值获取相应的枚举值。

请根据你的实际情况修改示例代码中的类名、属性字段名以及枚举类的相应方法。

Java: 将 List<String> 转换为枚举列表

原文地址: https://www.cveoy.top/t/topic/lhm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录