7 changed files with 187 additions and 11 deletions
			
			
		@ -0,0 +1,59 @@ | 
				
			|||
/** | 
				
			|||
 * Copyright (c) 2018 人人开源 All rights reserved. | 
				
			|||
 * | 
				
			|||
 * https://www.renren.io
 | 
				
			|||
 * | 
				
			|||
 * 版权所有,侵权必究! | 
				
			|||
 */ | 
				
			|||
 | 
				
			|||
package com.elink.esua.epdc.dto; | 
				
			|||
 | 
				
			|||
import java.io.Serializable; | 
				
			|||
import java.util.ArrayList; | 
				
			|||
import java.util.List; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * 树节点,所有需要实现树节点的,都需要继承该类 | 
				
			|||
 * | 
				
			|||
 * @author Mark sunlightcs@gmail.com | 
				
			|||
 * @since 1.0.0 | 
				
			|||
 */ | 
				
			|||
public class TreeNode<T> implements Serializable { | 
				
			|||
    private static final long serialVersionUID = 1L; | 
				
			|||
    /** | 
				
			|||
     * 主键 | 
				
			|||
     */ | 
				
			|||
    private String id; | 
				
			|||
    /** | 
				
			|||
     * 上级ID | 
				
			|||
     */ | 
				
			|||
    private String pid; | 
				
			|||
    /** | 
				
			|||
     * 子节点列表 | 
				
			|||
     */ | 
				
			|||
    private List<T> children = new ArrayList<>(); | 
				
			|||
 | 
				
			|||
    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 List<T> getChildren() { | 
				
			|||
        return children; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public void setChildren(List<T> children) { | 
				
			|||
        this.children = children; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
@ -0,0 +1,81 @@ | 
				
			|||
/** | 
				
			|||
 * Copyright (c) 2018 人人开源 All rights reserved. | 
				
			|||
 * | 
				
			|||
 * https://www.renren.io
 | 
				
			|||
 * | 
				
			|||
 * 版权所有,侵权必究! | 
				
			|||
 */ | 
				
			|||
 | 
				
			|||
package com.elink.esua.epdc.modules.moduleCategory.utils; | 
				
			|||
 | 
				
			|||
import com.elink.esua.epdc.commons.tools.validator.AssertUtils; | 
				
			|||
import com.elink.esua.epdc.dto.TreeNode; | 
				
			|||
 | 
				
			|||
import java.util.ArrayList; | 
				
			|||
import java.util.LinkedHashMap; | 
				
			|||
import java.util.List; | 
				
			|||
import java.util.Map; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * 树形结构工具类,如:菜单、部门等 | 
				
			|||
 * | 
				
			|||
 * @author Mark sunlightcs@gmail.com | 
				
			|||
 * @since 1.0.0 | 
				
			|||
 */ | 
				
			|||
public class TreeUtils { | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 根据pid,构建树节点 | 
				
			|||
     */ | 
				
			|||
    public static <T extends TreeNode> List<T> build(List<T> treeNodes, String pid) { | 
				
			|||
        //pid不能为空
 | 
				
			|||
        AssertUtils.isNull(pid, "pid"); | 
				
			|||
 | 
				
			|||
        List<T> treeList = new ArrayList<>(); | 
				
			|||
        for(T treeNode : treeNodes) { | 
				
			|||
            if (pid.equals(treeNode.getPid())) { | 
				
			|||
                treeList.add(findChildren(treeNodes, treeNode)); | 
				
			|||
            } | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
        return treeList; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 查找子节点 | 
				
			|||
     */ | 
				
			|||
    private static <T extends TreeNode> T findChildren(List<T> treeNodes, T rootNode) { | 
				
			|||
        for(T treeNode : treeNodes) { | 
				
			|||
            if(rootNode.getId().equals(treeNode.getPid())) { | 
				
			|||
                rootNode.getChildren().add(findChildren(treeNodes, treeNode)); | 
				
			|||
            } | 
				
			|||
        } | 
				
			|||
        return rootNode; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 构建树节点 | 
				
			|||
     */ | 
				
			|||
    public static <T extends TreeNode> List<T> build(List<T> treeNodes) { | 
				
			|||
        List<T> result = new ArrayList<>(); | 
				
			|||
 | 
				
			|||
        //list转map
 | 
				
			|||
        Map<String, T> nodeMap = new LinkedHashMap<>(treeNodes.size()); | 
				
			|||
        for(T treeNode : treeNodes){ | 
				
			|||
            nodeMap.put(treeNode.getId(), treeNode); | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
        for(T node : nodeMap.values()) { | 
				
			|||
            T parent = nodeMap.get(node.getPid()); | 
				
			|||
            if(parent != null && !(node.getId().equals(parent.getId()))){ | 
				
			|||
                parent.getChildren().add(node); | 
				
			|||
                continue; | 
				
			|||
            } | 
				
			|||
 | 
				
			|||
            result.add(node); | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
        return result; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue