java树状结构递归与非递归实现,外加sql递归实现

关于下列面出现的泛型自行修改

1、java8流式非递归实现

public Map<String, List<E>> selectCatalogue(int type, int status) {
     //获取所有数据
    List<E> catalogues=getPositionCatalogue(type, status);
    Map<String,List<E>> treeMap=new HashMap<>();
    List<E> levelOneMenus=new ArrayList<>();
    //利用java8流式新特性 将所用数据根据parentid的值进行分类
    //这样的话我们只需遍历parentid为零的数据也就是所谓的根节点
    //然后根据根节点的id在HashMap中找到对应的数据也就是他的孩子
   1. Map<Integer, List<E>> CatalogueMap=catalogues.stream()
       .collect(Collectors.groupingBy(E::getParentid,Collectors.toList()));
   2.Map<Integer, List<E>> CatalogueMap=catalogues.stream().
    map(item->{
    //如果拿到的数据不是我们最终的数据还需要进行设置其它属性用map处理一下,否则直接1就可以
          //比如下面这个E泛型可以会比处理的数据多个children
           //因为同事在一个类中没有children属性 而又创建了另外一个类来继承它
          E e=new E();
          BeanUtils.copyProperties(item,e);
           return e;     }).collect(Collectors.groupingBy(PositionCataloguePoJo::getParentid,Collectors.toList()));
    CatalogueMap.forEach((parentId,collect)->{
        if(Objects.equals(parentId,0)){
            levelOneMenus.addAll(collect);
        }
        collect.forEach(item->{
        //根据id进行匹配
            item.setChildren(CatalogueMap.get(item.getId()));
        });
    });
    treeMap.put("tree", levelOneMenus);
    return treeMap;
}

   对于这个继承这种耦合关系  我们尽量可以用聚合关系代替

2、java8流式非递归实现

public String selectCatalogue(int type,int status) {
         
        List<E> catalogues=getPositionCatalogue(type, status);
        List<E> levelOneMenus=catalogues.stream().filter(item->item.getParentid()==0)
                .peek(item->{
                    if(this.getChildren(item,catalogues).size()>0){
                        item.setChildren(this.getChildren(item,catalogues));
                    }
                }).collect(Collectors.toList());

        return JSONObject.toJSONString(levelOneMenus);
    }
    private List<E> getChildren(E root, List<E> catalogues) {
        return catalogues.stream().filter(item->
     //peek-返回的stream格式不会发生变化,但是可以对stream中的对象的某个属性进行赋值操作等 
     //map-必须有return 返回一个新的stream,如从一个对象流获取某个属性,返回属性的流
              Objects.equals(item.getParentid(), root.getId()))
                .peek((item)->{
                    List<E> children=getChildren(item,catalogues);
                    if (children.size()>0){
                        item.setChildren(children);
                    }
                }).collect(Collectors.toList());
    }

3、mybatis实现递归

<resultMap id="UsedResultMap" type="com.shkj.partsstock.pojo.PositionCataloguePoJo">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="grade" jdbcType="INTEGER" property="grade" />
    <result column="parentId" jdbcType="INTEGER" property="parentid" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="time" jdbcType="TIMESTAMP" property="time" />
    <collection property="children"
                select="xml的命名空间.selectUsedListChildren"
                column="{parentId=id,type=type,status=status}">
    </collection>
  </resultMap>

<select id="selectAllAndUses" resultMap="UsedResultMap" >
    select
    <include refid="Base_Column_List"/>
    from position_catalogue
    where status=#{status} and `type`=#{types}
    order by `name`
</select>

<select id="selectUsedListChildren" resultMap="UsedResultMap">
    select <include refid="Base_Column_List" />
    from position_catalogue
    where `parentId`=#{parentId,jdbcType=INTEGER} and `status` = #{status,jdbcType=INTEGER}
    and `type` = #{type,jdbcType=INTEGER}
  </select>

4、其他递归实现

public String selectCatalogues(int type,int status) {
      //获取所有数据       
      List<E> lists=getlist(type, status);
      //利用java8的新特性将所有数据中的parentid为0的数据
       List<E>  level=lists.stream().filter(item->
       item.getParentid()==0).collect(Collectors.toList());
       //同理将parentid不是零的数据拿到
       List<E>  levelChild=lists.stream().filter(item->
       item.getParentid()!=0).collect(Collectors.toList());
       遍历每一个parentid为零的,递归设置他的子数据
       for(E root:level){
            treePosition(root,levelChild);
       }
        return JSONObject.toJSONString(level);
}

//树实现
 private void treePosition(E root,  List<E>  lists)
{
        List<E> list=new ArrayList<>();
        for(E i:lists){
       //拿到lists里面的parentid等于root根节点id的数据
            if(i.getParentid().equals(root.getId())){
                list.add(i);
                treePosition(i,lists);
            }
        }
        if(!list.isEmpty()) {
            root.setChildren(list);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值