Java 实例 - 生成树结构工具类

  Java实例大全 

 代码:

package com.kc.ya.util;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kc.ya.model.po.TreeDto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 辰小白
 * @Date: 2020/2/24 10:56
 * @Email: 493820798@qq.com
 * @phone: 17673115513
 * @Version 1.0
 *
 * 生成树结构工具类
 */
public class TreeToolUtils {
    private List<TreeDto> rootList; //根节点对象存放到这里

    private List<TreeDto> bodyList; //其他节点存放到这里,可以包含根节点

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

    public List<TreeDto> getTree(){   //调用的方法入口
        if(bodyList != null && !bodyList.isEmpty()){
            //声明一个map,用来过滤已操作过的数据
            Map<String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getChild(beanTree,map));
        }
        return rootList;
    }

    public void getChild(TreeDto treeDto, Map<String,String> map){
        List<TreeDto> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getId()))
                .filter(c ->c.getPid().equals(treeDto.getId()))
                .forEach(c ->{
                    map.put(c.getId(),c.getPid());
                    getChild(c,map);
                    childList.add(c);
                });
        treeDto.setChildTreeDto(childList);

    }

      /**测试*/
    public static void main(String[] args) {
        TreeDto treeDto = new TreeDto("1", "总店", "null", "true",null);
        TreeDto treeDto1 = new TreeDto("2", "市分店", "1", "true",null);
        TreeDto treeDto2 = new TreeDto("3", "县分店", "2", "true",null);
        TreeDto treeDto3 = new TreeDto("710", "店长", "3", "true",null);
        TreeDto treeDto4= new TreeDto("713", "财务部", "3", "true",null);
        TreeDto treeDto5 = new TreeDto("20032", "后勤部", "3", "true",null);
        TreeDto treeDto6 = new TreeDto("1909", "小王", "710", "false",null);
        TreeDto treeDto7= new TreeDto("1974", "小张", "713", "false",null);
        TreeDto treeDto8 = new TreeDto("388187", "佳佳", "20032", "false",null);
        TreeDto treeDto9 = new TreeDto("1949", "阿达", "20032", "false",null);
        ArrayList<TreeDto> rootList = new ArrayList<>();//根节点
        ArrayList<TreeDto> bodyList = new ArrayList<>();//子节点
        rootList.add(treeDto);
        bodyList.add(treeDto1);
        bodyList.add(treeDto2);
        bodyList.add(treeDto3);
        bodyList.add(treeDto4);
        bodyList.add(treeDto5);
        bodyList.add(treeDto6);
        bodyList.add(treeDto7);
        bodyList.add(treeDto8);
        bodyList.add(treeDto9);
        TreeToolUtils utils =  new TreeToolUtils(rootList,bodyList);
        List<TreeDto> result =  utils.getTree();
        String jsonString = JSONObject.toJSONString(result.get(0));
        System.out.println(jsonString);
    }


}

测试结果:

{
	"childTreeDto": [{
		"childTreeDto": [{
			"childTreeDto": [{
				"childTreeDto": [{
					"childTreeDto": [],
					"id": "1909",
					"isParent": "false",
					"name": "小王",
					"pid": "710"
				}],
				"id": "710",
				"isParent": "true",
				"name": "店长",
				"pid": "3"
			}, {
				"childTreeDto": [{
					"childTreeDto": [],
					"id": "1974",
					"isParent": "false",
					"name": "小张",
					"pid": "713"
				}],
				"id": "713",
				"isParent": "true",
				"name": "财务部",
				"pid": "3"
			}, {
				"childTreeDto": [{
					"childTreeDto": [],
					"id": "388187",
					"isParent": "false",
					"name": "佳佳",
					"pid": "20032"
				}, {
					"childTreeDto": [],
					"id": "1949",
					"isParent": "false",
					"name": "阿达",
					"pid": "20032"
				}],
				"id": "20032",
				"isParent": "true",
				"name": "后勤部",
				"pid": "3"
			}],
			"id": "3",
			"isParent": "true",
			"name": "县分店",
			"pid": "2"
		}],
		"id": "2",
		"isParent": "true",
		"name": "市分店",
		"pid": "1"
	}],
	"id": "1",
	"isParent": "true",
	"name": "总店",
	"pid": "null"
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值