树级节点封装工具类

实体类

package com.ruoyi.common.core.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 程鹏
 * @date: 2021-06-28 16:44
 * @Description:  树形组件通用对象
 */
public class TreeItem implements Serializable {
    private String id;
    private String pId;
    private String name;
    private boolean checked;
    private String title;
    private List<TreeItem> children = new ArrayList<>();

    public TreeItem(String id, String pId, String name, boolean checked, String title) {
        this.id = id;
        this.pId = pId;
        this.name = name;
        this.checked = checked;
        this.title = title;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getpId() {
        return pId;
    }

    public void setpId(String pId) {
        this.pId = pId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<TreeItem> getChildren() {
        return children;
    }

    public void setChildren(List<TreeItem> children) {
        this.children = children;
    }
}

工具类

package com.ruoyi.common.utils;


import com.ruoyi.common.core.domain.TreeItem;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 程鹏
 * @date: 2021-06-28 16:37
 * @Description: 树形组件数据封装工具   多个父节点
 */
public class TreeUtil {

    private volatile static TreeUtil INSTANCE;

    // 构造方法私有化
    private TreeUtil() {

    }
    // 获取树工具单例 (DCL单例)
    public static TreeUtil getInstance() {
        if (INSTANCE == null) {
            synchronized (TreeUtil.class) {
                if (INSTANCE == null) {
                    INSTANCE = new TreeUtil();
                }
            }
        }
        return INSTANCE;
    }
    public TreeItem enquireTree(List<TreeItem> treeItemList) {
        if (treeItemList.isEmpty()) {
            return null;
        }
        // 过滤空对象
        List<TreeItem> treeItems = treeItemList.stream().filter(treeItem -> treeItem != null).collect(Collectors.toList());
        // 存储 id treeItem
        HashMap<String, TreeItem> itemMap = new HashMap<>();
        treeItems.forEach(treeItem -> {
            // k值为 树节点几点id
            itemMap.put(treeItem.getId(), treeItem);
        });
        // 声明一个变量存放根节点
        TreeItem root = null;
        // 遍历父节点
        // 数据组装
        for (TreeItem treeItem : treeItems) {
            String pid = treeItem.getpId();
            if ("0".equals(pid)) {
                // 说明该节点为根节点
                root = treeItem;
                continue;
            }
            // 获取父节点ip
            TreeItem parent = itemMap.get(pid);
            parent.getChildren().add(treeItem);
        }
        return root;
    }


}
posted @ 2021-11-27 10:50  &Melody  阅读(42)  评论(0编辑  收藏  举报