java组织树形结构

1.创建实体树

package com.hontye.parameter.entity.tree;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * tree TODO <br>
 * 
 * @author kangxu2 2017-1-7
 * 
 */
public class Tree<T> {
    /**
     * 节点ID
     */
    private String itemtype_id;
    /**
     * 显示节点文本
     */
    private String itemtype_name;
    /**
     * 节点状态,open closed
     */
    private String state = "open";
    /**
     * 节点是否被选中 true false
     */
    private boolean checked = false;
    /**
     * 节点属性
     */
    private List<Map<String, Object>> attributes;
    /**
     * 节点的子节点
     */
    private List<Tree<T>> children = new ArrayList<Tree<T>>();

    /**
     * 父ID
     */
    private String parent_itemtypeid;
    /**
     * 是否有父节点
     */
    private boolean isParent = false;
    /**
     * 是否有子节点
     */
    private boolean isChildren = false;



    public String getItemtype_id() {
		return itemtype_id;
	}

	public void setItemtype_id(String itemtype_id) {
		this.itemtype_id = itemtype_id;
	}

	public String getItemtype_name() {
		return itemtype_name;
	}

	public void setItemtype_name(String itemtype_name) {
		this.itemtype_name = itemtype_name;
	}

	public String getParent_itemtypeid() {
		return parent_itemtypeid;
	}

	public void setParent_itemtypeid(String parent_itemtypeid) {
		this.parent_itemtypeid = parent_itemtypeid;
	}

	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 List<Map<String, Object>> getAttributes() {
        return attributes;
    }

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

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

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

    public boolean isParent() {
        return isParent;
    }

    public void setParent(boolean isParent) {
        this.isParent = isParent;
    }

    public boolean isChildren() {
        return isChildren;
    }

    public void setChildren(boolean isChildren) {
        this.isChildren = isChildren;
    }


    public Tree(String itemtype_id, String itemtype_name, String state, boolean checked,
            List<Map<String, Object>> attributes, List<Tree<T>> children,
            boolean isParent, boolean isChildren, String parent_itemtypeid) {
        super();
        this.itemtype_id = itemtype_id;
        this.itemtype_name = itemtype_name;
        this.state = state;
        this.checked = checked;
        this.attributes = attributes;
        this.children = children;
        this.isParent = isParent;
        this.isChildren = isChildren;
        this.parent_itemtypeid = parent_itemtypeid;
    }

    public Tree() {
        super();
    }

    @Override
    public String toString() {
        
        return JSON.toJSONString(this);
    }

}

2.构建树

package com.hontye.parameter.entity.tree;


import java.util.ArrayList;
import java.util.List;


/**
 * 构建tree
 * TODO
 * <br>
 * @author kangxu2 2017-1-7
 *
 */
public class BuildTree {

    /**
     * 
     * TODO
     * <br>
     * @author kangxu2 2017-1-7
     *
     * @param nodes
     * @return
     */
    public static <T> Tree<T> build(List<Tree<T>> nodes) {

        if(nodes == null){
            return null;
        }
        List<Tree<T>> topNodes = new ArrayList<Tree<T>>();

        for (Tree<T> children : nodes) {

            String pid = children.getParent_itemtypeid();
            if (pid == null || "".equals(pid) || pid.equals("0")) {
                topNodes.add(children);

                continue;
            }

            for (Tree<T> parent : nodes) {
                String id = parent.getItemtype_id();
                if (id != null && id.equals(pid)) {
                    parent.getChildren().add(children);
                    children.setParent(true);
                    parent.setChildren(true);
                    parent.setAttributes(parent.getAttributes());
                    continue;
                }
            }

        }

        Tree<T> root = new Tree<T>();
        if (topNodes.size() == 1) {
            root = topNodes.get(0);
        } else {
            root.setItemtype_id("-1");
            root.setParent_itemtypeid("");
            root.setParent(false);
            root.setChildren(true);
            root.setChecked(true);
            root.setChildren(topNodes);
            root.setItemtype_name("顶级节点");
        }

        return root;
    }

}

3.以上两个类创建好了,就可以编写一个测试类,测试一下

package com.hontye.parameter.entity.tree;


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


public class BuildTreeTester {

    public static void main(String[] args) {
        
        
        List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
        List<Test> tests = new ArrayList<Test>();
        tests.add(new Test("0", "", "关于本人"));
        tests.add(new Test("1", "0", "技术学习"));
        tests.add(new Test("2", "0", "兴趣"));
        tests.add(new Test("3", "1", "JAVA"));
        tests.add(new Test("4", "1", "oracle"));
        tests.add(new Test("5", "1", "spring"));
        tests.add(new Test("6", "1", "springmvc"));
        tests.add(new Test("7", "1", "fastdfs"));
        tests.add(new Test("8", "1", "linux"));
        tests.add(new Test("9", "2", "骑行"));
        tests.add(new Test("10", "2", "吃喝玩乐"));
        tests.add(new Test("11", "2", "学习"));
        tests.add(new Test("12", "3", "String"));
        tests.add(new Test("13", "4", "sql"));
        tests.add(new Test("14", "5", "ioc"));
        tests.add(new Test("15", "5", "aop"));
        tests.add(new Test("16", "1", "等等"));
        tests.add(new Test("17", "2", "等等"));
        tests.add(new Test("18", "3", "等等"));
        tests.add(new Test("19", "4", "等等"));
        tests.add(new Test("20", "5", "等等"));

        for (Test test : tests) {
            Tree<Test> tree = new Tree<Test>();
            tree.setItemtype_id(test.getId());
            tree.setParent_itemtypeid(test.getPid());
            tree.setItemtype_name(test.getText());
            List<Map<String, Object>> lmp = new ArrayList<Map<String, Object>>();
            Map<String, Object> mp = new HashMap<String, Object>();
            mp.put("COSTDEVICE_NUMBER", "");
            mp.put("PRICE_PER", "");
            mp.put("ORDER_INDEX", "");
            mp.put("ADJUST_DATE", "");
            mp.put("IS_LEAF", "");
            lmp.add(mp);
            tree.setAttributes(lmp);
            trees.add(tree);
        }

        Tree<Test> t = BuildTree.build(trees);
        System.out.println(t);
    }
}

class Test {

    private String id;
    private String pid;
    private String text;
    
    private String costdevice_number;
    private String price_per;
    private String order_index;
    private String adjust_date;
    private String is_leaf;

    public String getId() {
        return id;
    }

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

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Test(String id, String pid, String text) {
        super();
        this.id = id;
        this.pid = pid;
        this.text = text;
    }
    
    

    public String getCostdevice_number() {
		return costdevice_number;
	}

	public void setCostdevice_number(String costdevice_number) {
		this.costdevice_number = costdevice_number;
	}

	public String getPrice_per() {
		return price_per;
	}

	public void setPrice_per(String price_per) {
		this.price_per = price_per;
	}

	public String getOrder_index() {
		return order_index;
	}

	public void setOrder_index(String order_index) {
		this.order_index = order_index;
	}

	public String getAdjust_date() {
		return adjust_date;
	}

	public void setAdjust_date(String adjust_date) {
		this.adjust_date = adjust_date;
	}

	public String getIs_leaf() {
		return is_leaf;
	}

	public void setIs_leaf(String is_leaf) {
		this.is_leaf = is_leaf;
	}

	public Test() {
        super();
    }

    @Override
    public String toString() {
        return "Test [id=" + id + ", pid=" + pid + ", text=" + text + "]";
    }

}

 4.运行后控制台输出如下:

{"checked":true,"children":[{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"0","itemtype_name":"关于本人","parent":false,"parent_itemtypeid":"","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"12","itemtype_name":"String","parent":true,"parent_itemtypeid":"3","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"18","itemtype_name":"等等","parent":true,"parent_itemtypeid":"3","state":"open"}],"itemtype_id":"3","itemtype_name":"JAVA","parent":true,"parent_itemtypeid":"1","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"13","itemtype_name":"sql","parent":true,"parent_itemtypeid":"4","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"19","itemtype_name":"等等","parent":true,"parent_itemtypeid":"4","state":"open"}],"itemtype_id":"4","itemtype_name":"oracle","parent":true,"parent_itemtypeid":"1","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"14","itemtype_name":"ioc","parent":true,"parent_itemtypeid":"5","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"15","itemtype_name":"aop","parent":true,"parent_itemtypeid":"5","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"20","itemtype_name":"等等","parent":true,"parent_itemtypeid":"5","state":"open"}],"itemtype_id":"5","itemtype_name":"spring","parent":true,"parent_itemtypeid":"1","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"6","itemtype_name":"springmvc","parent":true,"parent_itemtypeid":"1","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"7","itemtype_name":"fastdfs","parent":true,"parent_itemtypeid":"1","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"8","itemtype_name":"linux","parent":true,"parent_itemtypeid":"1","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"16","itemtype_name":"等等","parent":true,"parent_itemtypeid":"1","state":"open"}],"itemtype_id":"1","itemtype_name":"技术学习","parent":false,"parent_itemtypeid":"0","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"9","itemtype_name":"骑行","parent":true,"parent_itemtypeid":"2","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"10","itemtype_name":"吃喝玩乐","parent":true,"parent_itemtypeid":"2","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"11","itemtype_name":"学习","parent":true,"parent_itemtypeid":"2","state":"open"},{"attributes":[{"PRICE_PER":"","IS_LEAF":"","ADJUST_DATE":"","COSTDEVICE_NUMBER":"","ORDER_INDEX":""}],"checked":false,"children":[],"itemtype_id":"17","itemtype_name":"等等","parent":true,"parent_itemtypeid":"2","state":"open"}],"itemtype_id":"2","itemtype_name":"兴趣","parent":false,"parent_itemtypeid":"0","state":"open"}],"itemtype_id":"-1","itemtype_name":"顶级节点","parent":false,"parent_itemtypeid":"","state":"open"}

树形结构创建成功。

5.得到树形结构后,通过.toString()得到String字符串,再通过net.sf.json.JSONObject.fromObject(String string)获取到JsonObject对象做业务处理。如下: 

net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(costDeviceTree.toString());
                

 

    原文作者:java开发鼻祖
    原文地址: https://blog.csdn.net/qq_36004521/article/details/81482268
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞