跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 想问一个各位大牛技术问题,这个操作用 java8 分组怎么做?
未分類
25 9 月 2020

想问一个各位大牛技术问题,这个操作用 java8 分组怎么做?

想问一个各位大牛技术问题,这个操作用 java8 分组怎么做?

資深大佬 : thatiam92 0

有一个对象集合 Stream stream = Stream.of(new People(“张三”,”老师”),new People(“李四”,”学生”),new People(“王五”,”校长”));

想通过分组得类似于这样的分组 Map<Boolean,List<People>> ;

当为 true 时,List 集合为 {”张三”,”李四”,”王五”)

当为 false 时候,list 集合为{”张三老师”,”李四学生”,”王五校长”};

大佬有話說 (3)

  • 資深大佬 : lululau

    问题描述得一堆错误,尝试理解下你的意图:

    var people = List.of(new Person(“张三”,”老师”),new Person(“李四”,”学生”),new Person(“王五”,”校长”));
    var result = Map.of(true, people.stream().map(Person::getName).collect(Collectors.toList()),
    false, people.stream().map(Person::getNameWithRole).collect(Collectors.toList()));

  • 資深大佬 : lululau

    另外一个思路:

    var result = people.stream().flatMap(p -> Steam.of(Pair.of(true, p.getName()), Pair.of(false, p.getNameWithRole()))).collect(Collectors.groupingBy(Pair::getLeft, Collectors.mapping(Pair.getRight), Collectors.toList()))

  • 資深大佬 : lululau

    更正:

    var result = people.stream().flatMap(p -> Steam.of(Pair.of(true, p.getName()), Pair.of(false, p.getNameWithRole()))).collect(Collectors.groupingBy(Pair::getLeft, Collectors.mapping(Pair::getRight, Collectors.toList()))

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具