1.java中把list1集合拷贝到list2中
List<PoolCodeDto> list2= list1.stream()
.map(e -> {
PoolCodeDto codeDto = new PoolCodeDto();
BeanUtils.copyProperties(e, codeDto);
codeDto.setPoolCode(e.getDictCode());
codeDto.setH5Url(e.getDictValue());
codeDto.setWechatUrl(e.getDescription());
return codeDto;
}).collect(Collectors.toList());
2.Java中对象拷贝
/** 1、单个对象*/
public static <V> V copyBean(Object source, Class<V> clazz) {
/** 创建目标对象 实现属性拷贝*/
V result = null;
try {
result = clazz.newInstance();
/** 拷贝*/
Assert.notNull(source, "Source must not be null");
BeanUtils.copyProperties(source, result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
3.java中集合对象拷贝
/** 2、集合*/
public static <O, V> List<V> copyBeanList(List<O> list, Class<V> clazz) {
/** 创建目标对象 实现属性拷贝*/
return list.stream()
.map(o -> copyBean(o, clazz))
.collect(Collectors.toList());
}