根据数组生成tree节点

TEST.java

package com.haiyi.test;

import java.util.ArrayList;

import com.google.gson.Gson;
import com.haiyi.model.JiBieVO;
import com.haiyi.service.JiBieService;
import com.haiyi.util.TreeNode;
import com.ywb.messageDigest.MD5Digest;

public class Test {
    private static JiBieService jiBieService=new JiBieService();

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        getTreeNode();
        
        
    }
    
    public static void getTreeNode(){
        String[] a=new String[]{"101","201","301","401","501","601","701","801"};
        TreeNode node=new TreeNode();
        node.setId("101");
        node.setText("华为技术");
        for (int i = 0; i < a.length; i++) {
            String pid = node.getId();
            getTreeChild(node,pid,a[i],i);
        }
        
        String json=new Gson().toJson(node);
        System.out.println(json);
//        System.out.print("{\"info\":\"test\"," +
//                         "\"treeJson\":"+json+"}");

        String[] b=new String[]{"101","201","301","402"};
        
        for (int i = 0; i < b.length; i++) {
            if(b[i]==a[i]){
                continue;
            }
            String pid = node.getId();
            getTreeChild(node,pid,b[i],i);
        }
        String jsonb=new Gson().toJson(node);
        System.out.println(jsonb);
    }
    public static void getTreeChild(TreeNode node,String pid,String id,int i){
        if(i==1){
            ArrayList<TreeNode> children = node.getChildren();
            for (TreeNode treeNode : children) {
                if(treeNode.getId().equals(id)){
                    return;
                }
            }
            TreeNode child=new TreeNode(id,id);
            node.addChildren(child);
        }
        if(i==2){
            ArrayList<TreeNode> children = node.getChildren();
            for (TreeNode treeNode : children) {
                if(treeNode.getId().equals(id)){
                    return;
                }
            }
            TreeNode child=new TreeNode(id,id);
            node.getChildren().get(0)
                .addChildren(child);
        }
        if(i==3){
            ArrayList<TreeNode> children = node.getChildren();
            for (TreeNode treeNode : children) {
                if(treeNode.getId().equals(id)){
                    return;
                }
            }
            TreeNode child=new TreeNode(id,id);
            node.getChildren().get(0)
                .getChildren().get(0)
                .addChildren(child);
        }
        if(i==4){
            ArrayList<TreeNode> children = node.getChildren();
            for (TreeNode treeNode : children) {
                if(treeNode.getId().equals(id)){
                    return;
                }
            }
            TreeNode child=new TreeNode(id,id);
            node.getChildren().get(0)
                .getChildren().get(0)
                .getChildren().get(0)
                .addChildren(child);
        }
        if(i==5){
            ArrayList<TreeNode> children = node.getChildren();
            for (TreeNode treeNode : children) {
                if(treeNode.getId().equals(id)){
                    return;
                }
            }
            TreeNode child=new TreeNode(id,id);
            node.getChildren().get(0)
                .getChildren().get(0)
                .getChildren().get(0)
                .getChildren().get(0)
                .addChildren(child);
        }
    }

}

TtreeNode.java

package com.haiyi.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class TreeNode {

    private String text;
    private String id;
    private Map<String,Object> attributes;
    private boolean checked;
    private ArrayList<TreeNode> children;
    private String state;
    
    public TreeNode(){
        attributes=new HashMap<String,Object>();
        //state="closed";//默认关闭
        checked=false;
        children=new ArrayList<TreeNode>();
    }
    
    public TreeNode(String text, String id) {
        super();
        this.text = text;
        this.id = id;
        attributes=new HashMap<String,Object>();
        //state="closed";//默认关闭
        checked=false;
        children=new ArrayList<TreeNode>();
    }
    
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    
    public Map<String, Object> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = attributes;
    }

    
    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public boolean isChecked() {
        return checked;
    }
    public void setChecked(boolean checked) {
        this.checked = checked;
    }
    

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

    public void setChildren(ArrayList<TreeNode> children) {
        this.children = children;
    }

    /**
     * 添加子节点
     */
    public void addChildren(TreeNode node){
        children.add(node);
    }

    @Override
    public String toString() {
        return "TreeNode [text=" + text + ", id=" + id + ", attributes="
                + attributes + ", checked=" + checked + ", children="
                + children + ", state=" + state + "]";
    }
    
    
}

 

posted @ 2016-07-10 14:01  DaryllD  阅读(874)  评论(0编辑  收藏  举报