常规写法:
//不使用stream拷贝list
List<UserDTO> dtoList = new ArrayList<>();
for (User user : list) {
UserDTO d = new UserDTO();
BeanUtils.copyProperties(user, d);
dtoList.add(d);
}
使用java8的stream流的写法:
//使用stream拷贝list
List<UserDTO> dtoList = list.stream()
.map(e -> {
UserDTO d = new UserDTO();
BeanUtils.copyProperties(e, d);
return d;
})
.collect(Collectors.toList());
网友评论