java8 Stream 常用用法

              List<BspGroupDepartment> groupsByDeparIds = 
              groupService.getGroupsByDeparIds(departParentIds);  //負責父級部門的賬戶組id集合

                List<String> parentGroupIds = groupsByDeparIds.stream().map(one->one.getGroupId()).collect(Collectors.toList());
               // logger.info("負責父級部門的賬戶組id集合Id=" +JSON.toJSONString(parentGroupIds));
                List<BspPerson> persons = bspPersonService.getByGroupIds(parentGroupIds);
              //根據personId去重
                uniquePersons = persons.stream().collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(BspPerson::getPersonId))), ArrayList::new)
                );

 

 

 

//抽取對象集合中對象的id成id集合(List<String>)

List<BspGroupDepartment> groupsByDeparIds = New ArrayList<BspGroupDepartment>();

List<String> parentGroupIds = groupsByDeparIds.stream().map(one->one.getGroupId()).collect(Collectors.toList());

//根據一個屬性去重(personId)
      List<BspPerson>   uniquePersons = persons.stream().collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(BspPerson::getPersonId))), ArrayList::new)

 

//根據多個屬性去重

List<ClassEntity> distinctClass = classEntities.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getProfessionId() + ";" + o.getGrade()))), ArrayList::new));

發佈了9 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章