// Bean拷贝
public class BeanCopyUtils {
/**
* <V> 使用泛型
* V 返回值类型
* Class<V> 类对象
*
* @param source
* @param clazz
* @param <V>
* @return
*/
public static <V> V copyBean(Object source, Class<V> clazz) {
// 创建目标对象 反射
V result = null;
try {
// 使用反射new对象
result = clazz.newInstance();
// 实现属性拷贝
BeanUtils.copyProperties(source, result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* Stream流
* <O,V> 传入的泛型要求
*
* @param list
* @param clazz
* @param <O>
* @param <V>
* @return
*/
public static <O, V> List<V> copyBeanList(List<O> list, Class<V> clazz) {
/*List<HotArticleVo> articleVos = new ArrayList<>();
for (Article article : articles) {
HotArticleVo vo = new HotArticleVo();
BeanUtils.copyProperties(article, vo);
articleVos.add(vo);
}*/
return list.stream()
.map(o -> copyBean(o, clazz))
.collect(Collectors.toList());
}
}
10-25
08-15
4117
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交