一、flatMap简介
flatMap:将小Stream转换为大Stream
二、示例转换要求
目标:将如下对象中的分类category提取出来,去重。其中如"哲学,爱情",需要解析为两个分类,["哲学","爱情"]
[{"age":33,"books":[{"category":"哲学,爱情","id":1,"intro":"用一把刀划分了爱恨","name":"刀的两侧是光明与黑暗","score":88},{"category":"个人成长,爱情","id":2,"intro":"讲述如何从失败中明悟真理","name":"一个人不能死在同一把刀下","score":99}],"id":1,"intro":"一个从菜刀中明悟哲理的祖安人","name":"蒙多"},{"age":15,"books":[{"category":"哲学","id":3,"intro":"带你用思维去领略世界的尽头","name":"那风吹不到的地方","score":85},{"category":"哲学","id":3,"intro":"带你用思维去领略世界的尽头","name":"那风吹不到的地方","score":85},{"category":"爱情,个人传记","id":4,"intro":"一个哲学家的恋爱观注定很难把他所在的时代理解","name":"吹或不吹","score":56}],"id":2,"intro":"狂风也追逐不上他的思考速度","name":"亚拉索"},{"age":14,"books":[{"category":"爱情","id":5,"intro":"无法想象一个武者能对他的伴侣这么的宽容","name":"你的剑就是我的剑","score":56},{"category":"个人传记","id":6,"intro":"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?","name":"风与剑","score":100},{"category":"个人传记","id":6,"intro":"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?","name":"风与剑","score":100}],"id":3,"intro":"是这个世界在限制他的思维","name":"易"},{"age":14,"books":[{"category":"爱情","id":5,"intro":"无法想象一个武者能对他的伴侣这么的宽容","name":"你的剑就是我的剑","score":56},{"category":"个人传记","id":6,"intro":"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?","name":"风与剑","score":100},{"category":"个人传记","id":6,"intro":"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?","name":"风与剑","score":100}],"id":3,"intro":"是这个世界在限制他的思维","name":"易"}]
实现结果
["哲学","爱情","个人成长","个人传记"]
基础代码
作家
@Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode public class Author { private Long id; private String name; private Integer age; private String intro; private List<Book> books; }
书籍
@Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode public class Book { private Long id; private String name; //书籍分类 private String category; private Integer score; private String intro; }
private static List<Author> getAuthors() { //数据初始化 Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null); Author author2 = new Author(2L, "亚拉索", 15, "狂风也追逐不上他的思考速度", null); Author author3 = new Author(3L, "易", 14, "是这个世界在限制他的思维", null); Author author4 = new Author(3L, "易", 14, "是这个世界在限制他的思维", null); //书籍列表 List<Book> books1 = new ArrayList<>(); List<Book> books2 = new ArrayList<>(); List<Book> books3 = new ArrayList<>(); books1.add(new Book(1L, "刀的两侧是光明与黑暗", "哲学,爱情", 88, "用一把刀划分了爱恨")); books1.add(new Book(2L, "一个人不能死在同一把刀下", "个人成长,爱情", 99, "讲述如何从失败中明悟真理")); books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85, "带你用思维去领略世界的尽头")); books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85, "带你用思维去领略世界的尽头")); books2.add(new Book(4L, "吹或不吹", "爱情,个人传记", 56, "一个哲学家的恋爱观注定很难把他所在的时代理解")); books3.add(new Book(5L, "你的剑就是我的剑", "爱情", 56, "无法想象一个武者能对他的伴侣这么的宽容")); books3.add(new Book(6L, "风与剑", "个人传记", 100, "两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?")); books3.add(new Book(6L, "风与剑", "个人传记", 100, "两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?")); author.setBooks(books1); author2.setBooks(books2); author3.setBooks(books3); author4.setBooks(books3); List<Author> authorList = new ArrayList<>(Arrays.asList(author, author2, author3, author4)); return authorList; }
三、代码实现
1、代码
private static void test07() { //获取数据 List<Author> authors = getAuthors(); System.out.println(JSON.toJSONString(authors)); List<String> collect = authors.stream() //获取流 .flatMap(author -> author.getBooks().stream()) //获取作家书籍流,并合并 .distinct() //去重合并后的书籍 .flatMap(book -> Arrays.stream(book.getCategory().split(","))) //获取书籍分类,并将分类解析转换为流,合并 .distinct() //分类去重 .collect(Collectors.toList()); System.out.println(JSON.toJSONString(collect)); }
2、 执行过程