首页 > 技术文章 > Java——把list转为List<clazz>类型

shuhao66666 2021-07-15 20:01 原文

把list类型转为List<clazz> 类型,其中,值为null时转为空字符串

public static <T> List<T> listFomart(List list, Class<T> clazz) {
    List<T> result = new ArrayList<>();
    list.forEach(item -> {
        result.add(JSON.parseObject(JSON.toJSONString(item, SerializerFeature.WriteNullStringAsEmpty), clazz,  Feature.InitStringFieldAsEmpty));
    });
    return result;
}

推荐阅读