Java多级树工具类

===========================2020.7.7改进支持了泛型继承JSON显示================

 

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: perhamer
 * @date: 2020-07-07 14:31
 */
public class TreeUtils {

    public static > List getTree(List rootList, List childList) {
        List treeNodeList = new ArrayList<>();
        for (T node : rootList) {
            Set existsIds = new HashSet<>();
            List childrenTreeNodes = getChild(node.getNode(), childList, existsIds);
            node.setChildren(childrenTreeNodes);
            treeNodeList.add(node);
        }
        return treeNodeList;
    }

    private static > List getChild(String parentId, List childList, Set existsIds) {
        List children = new ArrayList<>();
        for (T node : childList) {
            if (existsIds.contains(node.getNode())) {
                continue;
            }
            if (node.getParent().equals(parentId)) {
                node.setChildren(getChild(node.getNode(), childList, existsIds));
                existsIds.add(node.getNode());
                children.add(node);
            }
        }
        return children;
    }
    @Data
    public static class TreeNode<T> {

        @JsonIgnore
        String node;

        @JsonIgnore
        String parent;

        List children;
    }
}



 

@Data
public class CADepartmentVO extends TreeUtils.TreeNode {

    private String id;

    private String name;

    private String allName;

}
 

============================以前的代码======================

public class TreeUtils {

    public static  List getTree(List rootList, List childList) {
        List treeNodeList = new ArrayList<>();
        for (T node : rootList) {
            TreeNode treeNode = new TreeNode();
            BeanUtils.copyProperties(node, treeNode);
            Set existsIds = new HashSet<>();
            List childrenTreeNodes = getChild(treeNode.getId(), childList, existsIds);
            treeNode.setChildren(childrenTreeNodes);
            treeNodeList.add(treeNode);
        }
        return treeNodeList;
    }

    public static  List getChild(int parentId, List childList, Set existsIds) {
        List childrens = new ArrayList<>();
        for (T node : childList) {
            if (existsIds.contains(node.getId())) {
                continue;
            }

            if (node.getParentId() == parentId) {
                TreeNode treeNode = new TreeNode();
                BeanUtils.copyProperties(node, treeNode);
                treeNode.setChildren(getChild(node.getId(), childList, existsIds));
                existsIds.add(node.getId());
                childrens.add(treeNode);
            }
        }
        return childrens;
    }

    @Data
    public static class TreeNode {

        Integer id;

        String name;

        Integer parentId;

        List children;
    }
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

 

你可能感兴趣的:(java)