当前位置:   article > 正文

Stream之flatMap_stream flatmap

stream flatmap

一、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":"易"}]

 实现结果

["哲学","爱情","个人成长","个人传记"]

 基础代码

作家

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @EqualsAndHashCode
  5. public class Author {
  6. private Long id;
  7. private String name;
  8. private Integer age;
  9. private String intro;
  10. private List<Book> books;
  11. }

 书籍

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @EqualsAndHashCode
  5. public class Book {
  6. private Long id;
  7. private String name;
  8. //书籍分类
  9. private String category;
  10. private Integer score;
  11. private String intro;
  12. }
  1. private static List<Author> getAuthors() {
  2. //数据初始化
  3. Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
  4. Author author2 = new Author(2L, "亚拉索", 15, "狂风也追逐不上他的思考速度", null);
  5. Author author3 = new Author(3L, "易", 14, "是这个世界在限制他的思维", null);
  6. Author author4 = new Author(3L, "易", 14, "是这个世界在限制他的思维", null);
  7. //书籍列表
  8. List<Book> books1 = new ArrayList<>();
  9. List<Book> books2 = new ArrayList<>();
  10. List<Book> books3 = new ArrayList<>();
  11. books1.add(new Book(1L, "刀的两侧是光明与黑暗", "哲学,爱情", 88, "用一把刀划分了爱恨"));
  12. books1.add(new Book(2L, "一个人不能死在同一把刀下", "个人成长,爱情", 99, "讲述如何从失败中明悟真理"));
  13. books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85, "带你用思维去领略世界的尽头"));
  14. books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85, "带你用思维去领略世界的尽头"));
  15. books2.add(new Book(4L, "吹或不吹", "爱情,个人传记", 56, "一个哲学家的恋爱观注定很难把他所在的时代理解"));
  16. books3.add(new Book(5L, "你的剑就是我的剑", "爱情", 56, "无法想象一个武者能对他的伴侣这么的宽容"));
  17. books3.add(new Book(6L, "风与剑", "个人传记", 100, "两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?"));
  18. books3.add(new Book(6L, "风与剑", "个人传记", 100, "两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?"));
  19. author.setBooks(books1);
  20. author2.setBooks(books2);
  21. author3.setBooks(books3);
  22. author4.setBooks(books3);
  23. List<Author> authorList = new ArrayList<>(Arrays.asList(author, author2, author3, author4));
  24. return authorList;
  25. }

三、代码实现

 1、代码

  1. private static void test07() {
  2. //获取数据
  3. List<Author> authors = getAuthors();
  4. System.out.println(JSON.toJSONString(authors));
  5. List<String> collect = authors.stream() //获取流
  6. .flatMap(author -> author.getBooks().stream()) //获取作家书籍流,并合并
  7. .distinct() //去重合并后的书籍
  8. .flatMap(book -> Arrays.stream(book.getCategory().split(","))) //获取书籍分类,并将分类解析转换为流,合并
  9. .distinct() //分类去重
  10. .collect(Collectors.toList());
  11. System.out.println(JSON.toJSONString(collect));
  12. }

2、 执行过程

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/479489
推荐阅读
相关标签
  

闽ICP备14008679号