List<Author>结构如下:
List<Reader>结构如下:
需求:将Author列表和Reader列表根据相同id合并到一个列表中
private static void mergeList() {
List<Author> authors = Author.getAuthors();
List<Reader> readers = Reader.getReaders();
// authors.forEach(System.out::println);
// readers.forEach(System.out::println);
authors.stream().flatMap(
author -> readers.stream()
.filter(reader -> reader.getId().equals(author.getId()))
.map(reader -> new MatchDto(
reader.getId(),
author.getName(),
reader.getName(),
author.getAge(),
reader.getAge(),
author.getIntro(),
author.getBooks()
))
)
.distinct()
.forEach(System.out::println);
}
结果如下: