List<Todo> list = new ArrayList<>();
list.add(new Todo(1,1,"hh","ss",LocalDateTime.now()));
list.add(new Todo(2,2,"hh","ss",LocalDateTime.now()));
list.add(new Todo(3,3,"hh","ss",LocalDateTime.now()));
List<TodoDto> todoDtos = new ArrayList<>();
todoDtos = list.stream().map(
todo ->new TodoDto(todo.getUserId(),todo.getTitle(),todo.getContent())
).collect(Collectors.toList());
// 或者
todoDtos = list.stream().map(
todo->{
TodoDto to = new TodoDto();
BeanUtils.copyProperties(todo,to);
return to;
}
).collect(Collectors.toList());
--------------------------------------
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TodoDto {
private int userId;
private String title;
private String content;
}
-----------------------------------------
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Todo {
private int id;
private int userId;
private String title;
private String content;
private LocalDateTime createTime;
}