java 列表数据转树形结构

目录

工具类

使用例子


工具类


/**
 * @Auther: jhwang
 * @Date: 2022/8/27 13:54
 * @Description:
 */
public class TreeUtils<T,M> {

    private T treeOne;

    public TreeUtils(T treeOne){
        this.treeOne = treeOne;
    }

    /**
     * 构架树并返回
     * @param list 集合
     * @param treeData 构建tree关键节点需要用到的数据
     * @return
     */
    public List<M> structureTreeData(List<M> list,TreeData<T,M> treeData){
        //不用递归,用分组排序的方法
        //return this.structureTree(this.treeOne,list,treeData);
        return this.groupSort(list,treeData);
    };

    /**
     * 分组排序的方法效率最高
     * @param list
     * @param treeData
     * @return
     */
    private List<M> groupSort(List<M> list,TreeData<T,M> treeData){
        //分组排序的方法
        ArrayList<M> ms = new ArrayList<>();
        Map<T, List<M>> map = list.stream().collect(Collectors.groupingBy(item ->
                treeData.getPid(item)
        ));
        map.forEach((pCode, value) -> {
            if (pCode.equals(this.treeOne)) {
                ms.addAll(value);
            }
            //遍历list通过code查出当前list设置children
            value.forEach(item -> {
                treeData.setChilds(item,map.get(treeData.getId(item)));
            });
        });
        return ms;
    }

    /**
     * 构建树形结构
     * @param pid 父系节点id
     * @param list 总的集合
     * @param treeData
     * @return
     */
    private List<M> structureTree(T pid,List<M> list,TreeData<T,M> treeData){
        List<M> outList = getList(pid,list,treeData);
        outList.forEach(item->{
            this.setChilds(item,treeData);//防止null指针
            treeData.setChilds(item,structureTree(treeData.getId(item),list,treeData));
        });
        return outList;
    }

    /**
     * 根据id设置子类并返回剩余的集合,并删除已经设置的节点
     * @param pid
     * @param list
     * @param treeData
     * @return
     */
    private List<M> getList(T pid, List<M> list,TreeData<T, M> treeData) {
        Iterator<M> iterator  = list.iterator();
        List<M> datas = new ArrayList<>();
        while (iterator.hasNext()) {
            M next = iterator.next();
            if(this.judgeEquals(pid,next,treeData)){
                //如果是主节点
                datas.add(next);
                iterator.remove();
            }
        }
        return datas;
    }


    /**
     * 判断id和pid是否相同
     * @param pid
     * @param m
     * @param treeData
     * @return
     */
    private boolean judgeEquals(T pid,M m,TreeData<T, M> treeData){
        if(pid instanceof String || pid instanceof Integer){
            return pid.equals(treeData.getPid(m));
        }
        throw new RuntimeException(TreeUtils.class+"什么鬼主键类型不识别");
    }

    private void setChilds(M m ,TreeData<T,M> treeData){
        if(treeData.getChilds(m) == null){
            treeData.setChilds(m,new ArrayList<M>());
        }
    }

    /**
     * 将树形结构转list
     * @return
     */
    public List<M> treeToList(List<M> treeList,TreeData<T,M> treeData){
        return flattenTreeToList(treeList,treeData,new ArrayList<M>());
    }

    public  List<M> flattenTreeToList(List<M> treeList,TreeData<T,M> treeData,List<M> result){
        if(treeList != null && treeList.size()>0){
            treeList.forEach(item->{
                result.add(item);
                flattenTreeToList(treeData.getChilds(item),treeData,result);
            });
        }
        return result;
    }

}

使用例子

List<CityData> cityData = new TreeUtils<String, CityData>("0")
				.structureTreeData(cityDataList, new TreeData<String, CityData>() {
			@Override
			protected String getId(CityData cityData) {
				return cityData.getCode();
			}

			@Override
			protected String getPid(CityData cityData) {
				return cityData.getPcode();
			}

			@Override
			protected List<CityData> getChilds(CityData cityData) {
				return cityData.getChildren();
			}

			@Override
			protected void setChilds(CityData cityData, List<CityData> list) {
				cityData.setChildren(list);
			}
		});

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值