组装树形结构数据,子节点去重!

  • 在组装树形结构数据时,为了减少递归查询数据库的次数,决定一次性将数据全部查出来,然后再内存中将数据组装成数据结构。但是数据承载的实体有限制,下文的TreeNode实体是一个简单的例子。
  • TreeNode实体
public class TreeNode {
    private String id;
    private String parentId;
    private HashSet<TreeNode> children;
    
    public TreeNode(String id, String parentId) {
        this.id = id;
        this.parentId = parentId;
    }


    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getId() {
        return id;
    }

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

    public HashSet<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(HashSet<TreeNode> children) {
        this.children = children;
    }
    
}
  1. 使用递归建造树结构
  /**
     * 使用递归方法建树
     * @param treeNodes
     * @return
     */
    public static List<TreeNode> buildTree(List<TreeNode> treeNodes) {
        List<TreeNode> trees = new ArrayList<>();
        for (TreeNode treeNode : treeNodes) {
            if ("0".equals(treeNode.getParentId())) {
                trees.add(findChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }


 public static TreeNode findChildren(TreeNode treeNode,List<TreeNode> treeNodes) {
        for (TreeNode it : treeNodes) {
            if(treeNode.getId().equals(it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new HashSet<TreeNode>());
                }
                treeNode.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return treeNode;
    }
  1. 组装数据
public static  List<TreeNode> packageData(){
        TreeNode treeNode1 = new TreeNode("1","0");
        TreeNode treeNode2 = new TreeNode("2","0");
        TreeNode treeNode3 = new TreeNode("3","2");
        TreeNode treeNode4 = new TreeNode("4","2");
        TreeNode treeNode5 = new TreeNode("5","2");
        TreeNode treeNode6 = new TreeNode("6","3");
        TreeNode treeNode7 = new TreeNode("7","6");
        TreeNode treeNode8 = new TreeNode("8","6");
        TreeNode treeNode9 = new TreeNode("9","1");
        TreeNode treeNode10 = new TreeNode("10","9");
        TreeNode treeNode11 = new TreeNode("11","9");
        TreeNode treeNode12 = new TreeNode("12","11");
        List<TreeNode> list = new ArrayList<>();
        list.add(treeNode1);
        list.add(treeNode2);
        list.add(treeNode3);
        list.add(treeNode4);
        list.add(treeNode5);
        list.add(treeNode6);
        list.add(treeNode7);
        list.add(treeNode8);
        list.add(treeNode9);
        list.add(treeNode10);
        list.add(treeNode11);
        list.add(treeNode12);
        return list;
    }
  • 测试
public static void main(String[] args) {

        List<TreeNode> treeNodes = packageData();

        List<TreeNode> treeList = TreeBuilder.buildTree(treeNodes);
        
    }
  • 小结:

    在TreeNode实体中,不要用List<TreeNode> 构造子节点,否则会出现重复。另外,此方法只试用与TreeNode单个java对象,下面的一篇博客组织树形节点数据的功能封装一个工具类,下篇博客见。
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值