后台处理树形数据工具类

直接上工具类
要求:实体类必有字段
id
pId

package com.yibuqiche.utils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yibuqiche.user.pojo.EpcCatalog;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 处理树形数据的工具类
 */
public class TreeToolUtils {
    //把查询到的根节点放到这下面
    private List<EpcCatalog> rootList;
    //把所有的非根节点方法这下面
    private List<EpcCatalog> bodyList;

    public TreeToolUtils(List<EpcCatalog> rootList, List<EpcCatalog> bodyList) {
        this.rootList = rootList;
        this.bodyList = bodyList;
    }

    //入口程序
    public List<EpcCatalog> getTree() {
        if (bodyList != null && !bodyList.isEmpty()) {
            Map<String, String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getChild(beanTree, map));
            return rootList;
        }
        return null;
    }

    public void getChild(EpcCatalog beanTree, Map<String, String> map) {
        List<EpcCatalog> childList = Lists.newArrayList();
        bodyList.stream().filter(c -> !map.containsKey(c.getId())).filter(c -> c.getpId().equals(beanTree.getId())).forEach(c -> {
            map.put(c.getId(), c.getpId());
            getChild(c, map);
            childList.add(c);
        });
        beanTree.setChildren(childList);
    }
    } 
    
    到此结束
    ------------------------------------------------------------------------
//测试数据
//    public static void main(String[] args) {
//        EpcCatalog beanTree1 = new EpcCatalog();
//        beanTree1.setCode("540000");
//        beanTree1.setLabel("西藏省");
//        beanTree1.setpId("100000");
//        EpcCatalog beanTree2 = new EpcCatalog();
//        beanTree2.setCode("540100");
//        beanTree2.setLabel("拉萨市");
//        beanTree2.setPid("540000");
//        EpcCatalog beanTree3 = new EpcCatalog();
//        beanTree3.setCode("540300");
//        beanTree3.setLabel("昌都市");
//        beanTree3.setPid("540000");
//        EpcCatalog beanTree4 = new EpcCatalog();
//        beanTree4.setCode("540121");
//        beanTree4.setLabel("林周县");
//        beanTree4.setPid("540100");
//        EpcCatalog beanTree5 = new EpcCatalog();
//        beanTree5.setCode("540121206");
//        beanTree5.setLabel("阿朗乡");
//        beanTree5.setPid("540121");
//        EpcCatalog beanTree6 = new EpcCatalog();
//        List<EpcCatalog> rootList = new ArrayList<>();
//        rootList.add(beanTree1);
//        List<EpcCatalog> bodyList = new ArrayList<>();
//        bodyList.add(beanTree1);
//        bodyList.add(beanTree2);
//        bodyList.add(beanTree3);
//        bodyList.add(beanTree4);
//        bodyList.add(beanTree5);
//        TreeToolUtils utils = new TreeToolUtils(rootList, bodyList);
//        List<EpcCatalog> result = utils.getTree();
//        result.get(0);
//    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值