树结构工具类

一:树结构实体

package com.it.tree;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 树状对象
 */
@Accessors(chain = true)
@Data
public class TreeData {

    /**
     * 节点标识
     */
    private String id;

    /**
     *  是否是父节点
     */
    private boolean isParent;

    /**
     * 父节点id
     */
    private String parentId;

    /**
     * 节点上所需要的属性(可根据需求增加和减少)
     */
    private String data1;

    /**
     * 节点上所需要的属性(可根据需求增加和减少)
     */
    private String data2;

    /**
     * 节点上所需要的属性(可根据需求增加和减少)
     */
    private String data3;

    /**
     * 节点上所需要的属性(可根据需求增加和减少)
     */
    private String data4;

    /**
     * 节点上所需要的属性(可根据需求增加和减少)
     */
    private String data5;

    /**
     * 子节点集合
     */
    private List childNodeList;

}

二:树结构工具类和main方法测试(封装数据)

package com.it.tree;

import com.google.common.collect.Lists;
import lombok.ToString;
import org.springframework.util.CollectionUtils;

import java.util.*;

@ToString
public class TreeUtils {


    /**
     * 通过构造器设置参数并处理返回树状数据
     * @return
     */
    public static List getResultTreeDataList(List rootNodeList, List childNodeList){
        /** 子节点不为空,封装到对应的根节点下*/
        if(!CollectionUtils.isEmpty(childNodeList)){
            /**创建一个map用于保存已经处理过的childNodeList中的TreeData的id*/
            Map childNodesHashMap = new HashMap<>(childNodeList.size());
            if(CollectionUtils.isEmpty(rootNodeList)){
                return Lists.newArrayList();
            }
            rootNodeList.forEach(rootTreeData -> getChildList(childNodeList,rootTreeData,childNodesHashMap));
            return rootNodeList;
        }
        return Lists.newArrayList();
    }

    private static void getChildList(List childNodeList,TreeData rootTreeData, Map childNodesHashMap) {
        /**用于保存子节点的数据*/
        List childList = new ArrayList<>();
        childNodeList.stream()
                /**过滤掉已经处理过的childTreeData数据*/
                .filter(childTreeData -> !childNodesHashMap.containsKey(childTreeData.getId()))
                /**过滤掉根节点的子节点*/
                .filter(childTreeData -> childTreeData.getParentId().equals(rootTreeData.getId()))
                .forEach(childTreeData ->{
                    childNodesHashMap.put(childTreeData.getId(),childTreeData.getParentId());
                    getChildList(childNodeList,childTreeData,childNodesHashMap);
                    childList.add(childTreeData);
                });
        /**将子节点放入对应父节点的集合中*/
        rootTreeData.setChildNodeList(childList);
    }


    public static void main(String[] args) {
        /******************************************测试案例*****************************************************/
        /**根节点集合*/
        List rootNodeList = new ArrayList<>();

        /**子节点集合*/
        List childNodeList = new ArrayList<>();

        /**科室*/
        TreeData rootTreeDataofDepartMent = new TreeData().setId("1").setParent(true).setParentId(null).setData1("科室").setData2("科室人数").setData3("科室配置");
        /**病区*/
        TreeData rootTreeDataofInpatient = new TreeData().setId("2").setParent(true).setParentId(null).setData1("病区").setData2("病区人数").setData3("病区配置");

        //根目录集合
        rootNodeList.add(rootTreeDataofDepartMent);
        rootNodeList.add(rootTreeDataofInpatient);

        /**科室*/
        TreeData childTreeDataofDepartMent1 = new TreeData().setId("11").setParent(true).setParentId("1").setData1("科室1").setData2("科室人数1").setData3("科室配置1");
        TreeData childTreeDataofDepartMent2 = new TreeData().setId("111").setParent(false).setParentId("11").setData1("科室11").setData2("科室人数11").setData3("科室配置11");
        childNodeList.add(childTreeDataofDepartMent1);
        childNodeList.add(childTreeDataofDepartMent2);

        /**病区*/
        TreeData childTreeDataofInpatient1 = new TreeData().setId("22").setParent(true).setParentId("2").setData1("病区2").setData2("病区人数2").setData3("病区配置2");
        TreeData childTreeDataofInpatient2 = new TreeData().setId("222").setParent(false).setParentId("22").setData1("病区22").setData2("病区人数22").setData3("病区配置22");
        childNodeList.add(childTreeDataofInpatient1);
        childNodeList.add(childTreeDataofInpatient2);

        List resultTreeDataList = TreeUtils.getResultTreeDataList(rootNodeList,childNodeList);
        System.err.println(resultTreeDataList.toString());

    }


}

三:执行main方法返回的结果(三层结构)

[TreeData(id=1, isParent=true, parentId=null, data1=科室, data2=科室人数, data3=科室配置, data4=null, data5=null, 
		  childNodeList=[TreeData(id=11, isParent=true, parentId=1, data1=科室1, data2=科室人数1, data3=科室配置1, data4=null, data5=null, 
								  childNodeList=[TreeData(id=111, isParent=false, parentId=11, data1=科室11, data2=科室人数11, data3=科室配置11, data4=null, data5=null, childNodeList=[])])]), 
 TreeData(id=2, isParent=true, parentId=null, data1=病区, data2=病区人数, data3=病区配置, data4=null, data5=null, 
		  childNodeList=[TreeData(id=22, isParent=true, parentId=2, data1=病区2, data2=病区人数2, data3=病区配置2, data4=null, data5=null, 
								  childNodeList=[TreeData(id=222, isParent=false, parentId=22, data1=病区22, data2=病区人数22, data3=病区配置22, data4=null, data5=null, childNodeList=[])])])]

你可能感兴趣的:(工具类,java)