Java List与树的互转


平时工作中都会遇到包含层级关系的List数据转换成树形结构,或者数据已是树形结构了,需要我们处理成普通的单层list结构。以下代码均为本人实际开发所写代码,可能不是最优解、复杂度也比较高,在此和大家一起分享学习!   注:该工具类支持将list转换成树/森林。可自行测试,有疑问或更优方案,可私聊我。     TreeNode
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TreeNode {

    private String id;

    private String key;

    private String value;

   private String name; private String parentId; private String createName; private LocalDateTime createTime; private List> children; private boolean hasParent = false; private boolean hasChildren = false; public void initChildren() { this.children = new ArrayList<>(); } }
TreeUtil
public class TreeUtil {

    protected TreeUtil() {

    }

    private final static String TOP_NODE_ID = "0";



    /**
     * 使用递归方法建树
     *
     * @param nodes
     * @return
     */
    public static List> buildByRecursive(List> nodes) {
        List> trees = new ArrayList<>();
        for (TreeNode treeNode : nodes) {
            if (TOP_NODE_ID.equals(treeNode.getParentId())) {
                trees.add(findChildren(treeNode,nodes));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    public static TreeNode findChildren(TreeNode treeNode, List> treeNodes) {
        for (TreeNode it : treeNodes) {
            if (treeNode.getId().equals(it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList>());
                }
                treeNode.getChildren().add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    public static void buildTrees(List> trees, List list) {
        list.forEach(entity -> {
            TreeNode tree = new TreeNode<>();
            tree.setId(entity.getId());
            tree.setKey(entity.getId());
            tree.setValue(entity.getId());
            tree.setName(entity.getName());
            tree.setParentId(entity.getParentId());
            tree.setCreateTime(entity.getCreateTime());
            tree.setCreateName(entity.getCreateName());
            trees.add(tree);
        });
    }

    public static  List> buildTreeList(List> nodes) {
        if (nodes == null) {
            return null;
        }
        List> topNodes = new ArrayList<>();
        //利用两层循环,组装树/森林结构
        nodes.forEach(first -> {
            String pid = first.getParentId();
            first.setHasParent(true);
            if (pid == null || TOP_NODE_ID.equals(pid)) {
                topNodes.add(first);
                first.setHasParent(false);
            } else {
                boolean isExistPid = false;
                for (TreeNode temp : nodes) {
                    if (temp.getId().equals(pid)) {
                        isExistPid = true;
                    }
                }
                if (!isExistPid) {
                    topNodes.add(first);
                    first.setHasParent(false);
                }
            }
            for (TreeNode second : nodes) {
                String id = second.getId();
                if (id != null && id.equals(pid)) {
                    if (second.getChildren() == null) {
                        second.initChildren();
                    }
                    second.getChildren().add(first);
                    first.setHasParent(true);
                    second.setHasChildren(true);
                    second.setHasParent(true);
                    return;
                }
            }
        });
        return topNodes;
    }

    public static  TreeNode buildTree(List> nodes) {
        if (nodes == null) {
            return null;
        }
        List> topNodes = new ArrayList<>();
        //利用两层循环,组装树/森林结构
        nodes.forEach(first -> {
            String pid = first.getParentId();
            first.setHasParent(true);
            if (pid == null || TOP_NODE_ID.equals(pid)) {
                topNodes.add(first);
                first.setHasParent(false);
            } else {
                boolean isExistPid = false;
                for (TreeNode temp : nodes) {
                    if (temp.getId().equals(pid)) {
                        isExistPid = true;
                    }
                }
                if (!isExistPid) {
                    topNodes.add(first);
                    first.setHasParent(false);
                }
            }
            for (TreeNode second : nodes) {
                String id = second.getId();
                if (id != null && id.equals(pid)) {
                    if (second.getChildren() == null) {
                        second.initChildren();
                    }
                    second.getChildren().add(first);
                    first.setHasParent(true);
                    second.setHasChildren(true);
                    second.setHasParent(true);
                    return;
                }
            }
        });
        TreeNode root = new TreeNode<>();
        root.setId("0");
        root.setParentId("");
        root.setHasParent(false);
        root.setHasChildren(true);
        root.setChildren(topNodes);
        root.setName("root");
        return root;
    }


    public static  List> tree2List(List>  treeList) {
        List> list = new ArrayList<>();
        for (TreeNode tree : treeList) {
            List> child = tree.getChildren();
            list.add(tree);
            if (child != null && child.size() > 0) {
                list.addAll(tree2List(child));
                tree.setChildren(null);
            }
        }
        return list;
    }
}