java8使用stream流将数据处理成树状结构(非递归)三

看视频学习,有个地方需要返回这样的效果,Map类型,key为一级分类的id,value是集合数组:
在这里插入图片描述
视频中用的是循环查库,所以想着用别的方法来写

实体类格式
Catelog2Vo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Catelog2Vo {
    private String catelog1Id;  // 一级分类的catId
    private List<Catelog3Vo> catelog3List;
    private String id;
    private String name;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Catelog3Vo{
        private String catelog2Id;  // 二级分类的catId
        private String id;
        private String name;
    }
}
CategoryEntity
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
	private static final long serialVersionUID = 1L;

	@TableId
	private Long catId;

	private String name;

	private Long parentCid;

	private Integer catLevel;

	@TableLogic(value = "1", delval = "0")
	private Integer showStatus;
	
	private Integer sort;

	private String icon;

	private String productUnit;

	private Integer productCount;

	@TableField(exist = false)
	private List<CategoryEntity> children;
}
代码

这是视频中的写法,循环查库,个人认为循环查库最好不要,除非数据量小

public Map<String,List<Catelog2Vo>> getCatalogJson() {
/**************************** 耗时2-300ms ****************************/
// 查出所有一级分类
  List<CategoryEntity> category1List = baseMapper.selectList(new QueryWrapper<CategoryEntity>()
          .eq("parent_cid",0));
  Map<String, List<Catelog2Vo>> resultData = category1List.stream()
          .collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
      // 根据一级分类的catId查出二级分类
      List<CategoryEntity> category2List = baseMapper.selectList(new QueryWrapper<CategoryEntity>()
              .eq("parent_cid", v.getCatId()));
      // 封装二级分类数据
      List<Catelog2Vo> category2Data = category2List.stream().map(l2 -> {
          Catelog2Vo catelog2Vo = new Catelog2Vo(l2.getParentCid().toString(),
                  null,l2.getCatId().toString(),
                  l2.getName());
          // 根据二级分类的catId查出三级分类
          List<CategoryEntity> category3List = baseMapper.selectList(new QueryWrapper<CategoryEntity>()
                  .eq("parent_cid", l2.getCatId()));
          if (category3List != null) {
              List<Catelog2Vo.Catelog3Vo> category3Data = category3List.stream().map(l3 -> {
                  Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(l3.getParentCid().toString(),
                          l3.getCatId().toString(),
                          l3.getName());
                  return catelog3Vo;
              }).collect(Collectors.toList());
              catelog2Vo.setCatelog3List(category3Data);
          }
          return catelog2Vo;
      }).collect(Collectors.toList());
      return category2Data;
  }));
  return resultData;
}

修改过后的写法,一次性查出所有分类,再通过stream流组装数据:

public Map<String,List<Catelog2Vo>> getCatalogJson() {
/**************************** 耗时1-20ms ****************************/
   List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);
   // 所有一级分类
   List<CategoryEntity> category1Data = categoryEntityList.stream()
           .filter(item -> item.getParentCid() == 0)
           .collect(Collectors.toList());

   Map<String, List<Catelog2Vo>> data = category1Data.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
       List<Catelog2Vo> catelog2Vos = categoryEntityList.stream()
               .filter(f2 -> f2.getParentCid() == v.getCatId())
               .map(lv2 -> {
                   List<Catelog2Vo.Catelog3Vo> catelog3Vos = categoryEntityList.stream()
                           .filter(f3 -> f3.getParentCid() == lv2.getCatId())
                           .map(lv3 -> {
                               Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo();
                               catelog3Vo.setCatelog2Id(lv3.getParentCid().toString());
                               catelog3Vo.setName(lv3.getName());
                               catelog3Vo.setId(lv3.getCatId().toString());
                               return catelog3Vo;
                           }).collect(Collectors.toList());
                   Catelog2Vo catelog2Vo = new Catelog2Vo();
                   catelog2Vo.setCatelog1Id(lv2.getParentCid().toString());
                   catelog2Vo.setName(lv2.getName());
                   catelog2Vo.setId(lv2.getCatId().toString());
                   catelog2Vo.setCatelog3List(catelog3Vos);
                   return catelog2Vo;
               }).collect(Collectors.toList());
       return catelog2Vos;
   }));
   return data;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值