list 转树 tree 的三种写法

@Data
public class TreeNode {

    private Integer id;
    private Integer pid;
    private String name;
    private List<TreeNode> children;

    public TreeNode(Integer id, Integer pid, String name) {
        this.id = id;
        this.pid = pid;
        this.name = name;
    }
}
@RestController
@RequestMapping("/Department")
public class testController {

    @GetMapping("menu")
    public  Object  menu(){
        List<TreeNode> list = new ArrayList<TreeNode>();
        list.add(new TreeNode(1, 0, "1"));
        list.add(new TreeNode(2, 0, "2"));
        list.add(new TreeNode(3, 2, "3"));
        list.add(new TreeNode(4, 3, "4"));
        list.add(new TreeNode(5, 4, "5"));
        list.add(new TreeNode(6, 5, "6"));

        List<TreeNode> treeList = new ArrayList<TreeNode>();
        List<TreeNode> treeList1 = new ArrayList<TreeNode>();
        List<TreeNode> treeList2 = new ArrayList<TreeNode>();
        List<TreeNode> treeList3 = new ArrayList<TreeNode>();
        //方法一、
//        treeList = listGetStree(list);
//        treeList = listToTree(list);
        treeList = toTree(list);


        return treeList;
    }

    private static List<com.example.demo.TreeNode> listGetStree(List<com.example.demo.TreeNode> list) {
        List<com.example.demo.TreeNode> treeList = new ArrayList<com.example.demo.TreeNode>();
        for (com.example.demo.TreeNode tree : list) {
            //找到根
            if (tree.getPid() == 0) {
                treeList.add(tree);
                //找到子

            }
            for (com.example.demo.TreeNode treeNode : list) {
                if (treeNode.getPid() == tree.getId()) {
                    if (tree.getChildren() == null) {
                        tree.setChildren(new ArrayList<com.example.demo.TreeNode>());
                    }
                    tree.getChildren().add(treeNode);
                }
            }
        }
        return treeList;
    }

    /**
     * 方法二、
     *
     * @param list
     * @return
     */
    public static List<com.example.demo.TreeNode> listToTree(List<com.example.demo.TreeNode> list) {
        //用递归找子。
        List<com.example.demo.TreeNode> treeList = new ArrayList<com.example.demo.TreeNode>();
        for (com.example.demo.TreeNode tree : list) {
            if (tree.getPid() == 0) {
                treeList.add(findChildren(tree, list));
            }
        }
        return treeList;
    }

    private static com.example.demo.TreeNode findChildren(com.example.demo.TreeNode tree, List<com.example.demo.TreeNode> list) {
        for (com.example.demo.TreeNode node : list) {
            if (node.getPid() == tree.getId()) {
                if (tree.getChildren() == null) {
                    tree.setChildren(new ArrayList<com.example.demo.TreeNode>());
                }
                tree.getChildren().add(findChildren(node, list));
            }
        }
        return tree;
    }

    /**
     * 方法三
     *
     * @param list
     * @return
     */
    private static List<com.example.demo.TreeNode> toTree(List<com.example.demo.TreeNode> list) {
        List<com.example.demo.TreeNode> treeList = new ArrayList<com.example.demo.TreeNode>();
        for (com.example.demo.TreeNode tree : list) {
            if (tree.getPid() == 0) {
                treeList.add(tree);
            }
        }
        for (com.example.demo.TreeNode tree : list) {
            toTreeChildren(treeList, tree);
        }
        return treeList;
    }

    private static void toTreeChildren(List<com.example.demo.TreeNode> treeList, com.example.demo.TreeNode tree) {
        for (com.example.demo.TreeNode node : treeList) {
            if (tree.getPid() == node.getId()) {
                if (node.getChildren() == null) {
                    node.setChildren(new ArrayList<com.example.demo.TreeNode>());
                }
                node.getChildren().add(tree);
            }
            if (node.getChildren() != null) {
                toTreeChildren(node.getChildren(), tree);
            }
        }
    }
}
输出结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值