stream日常操作

list放到另一个list中 (分组)

我有个班级 集合 有个学生集合,需要把对应的班级对应的学生放进来

        List<RvFacilityRoom> rvFacilityRoomList = this.selectList();
        //然后对list进行分组 转换为map key为班级id
        Map<int, List<student>> studentMap= studentList.stream().collect(Collectors.groupingBy(x -> x.greadeId()));
//greadeModels是已经查出来的班级的集合

  greadeModels.forEach(x -> {
            if (rvFacilityRoomMap.get(x.getId()) != null) {
                x.setRvFacilityRoomList(rvFacilityRoomMap.get(x.getId()));
            }
        });

这样就可以了

两个list合并

            List<BigDecimal> res = Stream.of(list1, list2).flatMap(Collection::stream).collect(Collectors.toList());

你可能感兴趣的:(java,基础,stream,java)