根据数组生成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 + "]";
    }
    
    
}

 

转载于:https://www.cnblogs.com/super-admin/p/treeNode.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值