list转树JSON

node.java:


import java.util.List;




public class Node {
private static final long serialVersionUID = -2721191232926604726L;


    private int id;


    private int parentId;


    private Node parent;


    private List<Node> children;


    private String name;


    private int level;


    private int sort;


    private int rootId;


    private String type;


    private boolean isLeaf;


    private String description;


    public Node() {
        super();
    }


    public Node(int id, int parentId, String name) {
        super();
        this.id = id;
        this.parentId = parentId;
        this.name = name;
    }


    public String getDescription() {
        return description;
    }


    public void setDescription(String description) {
        this.description = description;
    }


    public int getId() {
        return id;
    }


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


    public Node getParent() {
        return parent;
    }


    public void setParent(Node parent) {
        this.parent = parent;
    }


    public int getParentId() {
        return parentId;
    }


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


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public int getLevel() {
        return level;
    }


    public void setLevel(int level) {
        this.level = level;
    }


    public String getType() {
        return type;
    }


    public List<Node> getChildren() {
        return children;
    }


    public void setChildren(List<Node> children) {
        this.children = children;
    }


    public void setType(String type) {
        this.type = type;
    }


    public boolean isLeaf() {
        return isLeaf;
    }


    public void setLeaf(boolean isLeaf) {
        this.isLeaf = isLeaf;
    }


    public int getSort() {
        return sort;
    }


    public void setSort(int sort) {
        this.sort = sort;
    }


    public int getRootId() {
        return rootId;
    }


    public void setRootId(int rootId) {
        this.rootId = rootId;
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + parentId;
        return result;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Node other = (Node) obj;
        if (id != other.id)
            return false;
        if (parentId != other.parentId)
            return false;
        return true;
    }


    @Override
    public String toString() {
        return "Node {id=" + id + ", parentId=" + parentId + ", children="
                + children + ", name=" + name + ", level =" + level + "}";
    }
}




Test.java:

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class Test {
@SuppressWarnings("unchecked")
    private List<Node> buildListToTree(List<Node> dirs) {
        List<Node> roots = findRoots(dirs);
        List<Node> notRoots = (List<Node>) CollectionUtils
                .subtract(dirs, roots);
        for (Node root : roots) {
            root.setChildren(findChildren(root, notRoots));
        }
        return roots;
    }


    public List<Node> findRoots(List<Node> allNodes) {
        List<Node> results = new ArrayList<Node>();
        for (Node node : allNodes) {
            boolean isRoot = true;
            for (Node comparedOne : allNodes) {
                if (node.getParentId() == comparedOne.getId()) {
                    isRoot = false;
                    break;
                }
            }
            if (isRoot) {
                node.setLevel(0);
                results.add(node);
                node.setRootId(node.getId());
            }
        }
        return results;
    }


    @SuppressWarnings("unchecked")
    private List<Node> findChildren(Node root, List<Node> allNodes) {
        List<Node> children = new ArrayList<Node>();


        for (Node comparedOne : allNodes) {
            if (comparedOne.getParentId() == root.getId()) {
                comparedOne.setParent(root);
                comparedOne.setLevel(root.getLevel() + 1);
                children.add(comparedOne);
            }
        }
        List<Node> notChildren = (List<Node>) CollectionUtils.subtract(allNodes, children);
        for (Node child : children) {
            List<Node> tmpChildren = findChildren(child, notChildren);
            if (tmpChildren == null || tmpChildren.size() < 1) {
                child.setLeaf(true);
            } else {
                child.setLeaf(false);
            }
            child.setChildren(tmpChildren);
        }
        return children;
    }


    public static void main(String[] args) {
    Test tb = new Test();
        List<Node> allNodes = new ArrayList<Node>();
        allNodes.add(new Node(1, 0, "节点1"));
        allNodes.add(new Node(2, 0, "节点2"));
        allNodes.add(new Node(3, 0, "节点3"));
        allNodes.add(new Node(11, 7, "节点11"));
        allNodes.add(new Node(4, 1, "节点4"));
        allNodes.add(new Node(5, 1, "节点5"));
        allNodes.add(new Node(6, 1, "节点6"));
        allNodes.add(new Node(7, 4, "节点7"));
        allNodes.add(new Node(8, 4, "节点8"));
        allNodes.add(new Node(9, 5, "节点9"));
        allNodes.add(new Node(10, 100, "节点10"));
        List<Node> roots = tb.buildListToTree(allNodes);
        JSONArray array = new JSONArray();
        for (Node node : roots) {
        System.out.println(node.getName());
        JSONObject o = new JSONObject();
        o.put("id", node.getId());
        o.put("name", node.getName());
        List<Node> child = node.getChildren();
        array.add(o);      
        if(child!=null && child.size() > 0){
        tb.fillChildren(o, child);
        }
}
        System.out.println(array);
        
    }
    private void fillChildren(JSONObject o, List<Node> childs) {
    JSONArray array = new JSONArray();
    for (Node node : childs) {
    JSONObject oo = new JSONObject();
        List<Node> child = node.getChildren();
        oo.put("id", node.getId());
        oo.put("name", node.getName());  
        System.out.println(node.getName());
        array.add(oo); 
        if(child!=null && child.size() > 0){
        fillChildren(oo, child);
        }
}
    o.put("child", array);
   
    }
    
    
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值