java树形工具类 泛型 通用非递归

2023-10-22 15:04:01 浏览数 (2)

1、TreeData:供实体类继承

代码语言:javascript复制
import java.util.ArrayList;
import java.util.List;

public class TreeData<T,E> {
    public E id;

    public E pid;

    public List<T> child=new ArrayList<>();

}

2、TreeUtil:实现工具类

代码语言:javascript复制
import java.util.*;

/**
 * @author YZX
 */

/***
 *
 * @param <T> 数据类 类型
 * @param <E> id pid数据类型
 */
public class TreeUtil<T extends TreeData<T, E>, E> {

    private List<T> treeData;

    private Map<E, List<T>> map;

    private String rootStringValue;

    /***
     *
     * @param data 数据源
     * @param rootStringValue 指定根节点标识,例如系统中pid为“0”的是根节点
     */
    public TreeUtil(List<T> data,String rootStringValue) {
        this.treeData = data;
        this.map = new HashMap<>(data.size());
        this.rootStringValue = rootStringValue;
        for (T datum : data) {
            List<T> ts = this.map.get(datum.pid);
            if (null == ts) {
                ts = new ArrayList<T>();
                map.put(datum.pid, ts);
            }
            ts.add(datum);
        }
    }

    public TreeUtil() {
    }

    /***
     * 构建树
     * @return
     */
    public List<T> getTreeList() {
        List<T> resultData = new ArrayList<>();
        Map<E, List<T>> map = this.map;
        List<T> treeData = this.treeData;
        for (T treeDatum : treeData) {
            E id = treeDatum.id;
            List<T> ts = map.get(id);
            if (null != ts) {
                treeDatum.child = ts;
            }
            if (rootStringValue.equals(String.valueOf(treeDatum.pid))) {
                resultData.add(treeDatum);
            }
        }

        return resultData;
    }
}

构建对象:

代码语言:javascript复制
public class Dept  extends TreeData<Dept,Integer> {

    public String name;


    public Dept(int id, String name, int pid) {
        super.id = id;
        this.name = name;
        super.pid = pid;
    }

    public Dept() {
    }


    @Override
    public String toString() {
        return "Dept{"  
                "id="   id  
                ", name='"   name   '''  
                ", pid="   pid  
                ", child="   child  
                '}';
    }
}

测试案例

代码语言:javascript复制
Dept 技术部 = new Dept(2, "技术部", 1);
        Dept 市场部 = new Dept(3, "市场部", 1);
        Dept 产品部 = new Dept(4, "产品部", 1);
        Dept 开发部 = new Dept(5, "开发部", 2);
        Dept 运维部 = new Dept(6, "运维部", 2);
        Dept 投标部 = new Dept(7, "投标部", 3);
        Dept 招标部 = new Dept(8, "招标部", 3);
        Dept 前端部门 = new Dept(9, "前端部门", 5);
        Dept 后端部门 = new Dept(10, "后端部门", 5);
        Dept zzz = new Dept(11, "zzz", 9);
        Dept xxx = new Dept(12, "xxx", 11);

        List<Dept> a=new ArrayList<>();
        a.add(技术部);
        a.add(市场部);
        a.add(产品部);
        a.add(开发部);
        a.add(运维部);
        a.add(投标部);
        a.add(招标部);
        a.add(前端部门);
        a.add(后端部门);
        a.add(zzz);
        a.add(xxx);

TreeUtil<Dept,Integer> treeUtil = new TreeUtil(a,"1");
List<Dept> treeList = treeUtil.getTreeList();
treeList.forEach(System.out::println);

0 人点赞