java递归树状json

更新时间:2018-08-08 10:00:23 点击次数:1443次

一、建立递归树的实体类

public class Tree implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 2944880335559089140L;

private String key;//节点key

private String title;//节点名称

private String parentKey;//节点父id

private String iconCls = "folder";//节点样式,默认即可

private Boolean isLeaf = true;//是否为叶子节点,true表示是叶子节点,false表示不是叶子节点

private Boolean expanded = true; //是否展开,默认true,展开

private List<Tree> children;//孩子节点

/**
 * @取得id的值
 * @return the id
 */
public String getKey() {
    return key;
}

/**
 * @设置id的值
 * @param id the id to set
 */
public void setKey(String key) {
    this.key = key;
}

/**
 * 取得title的值
 *
 * @return  title值
 */

public String getTitle() {
    return title;
}

/**
 * 设定title的值
 *
 * @param  title 设定值
 */
public void setTitle(String title) {
    this.title = title;
}

/**
 * @取得parentId的值
 * @return the parentId
 */
public String getParentKey() {
    return parentKey;
}

/**
 * @设置parentId的值
 * @param parentId the parentId to set
 */
public void setParentKey(String parentKey) {
    this.parentKey = parentKey;
}

/**
 * @取得iconCls的值
 * @return the iconCls
 */
public String getIconCls() {
    return iconCls;
}

/**
 * @设置iconCls的值
 * @param iconCls the iconCls to set
 */
public void setIconCls(String iconCls) {
    this.iconCls = iconCls;
}

/**
 * @取得isLeaf的值
 * @return the isLeaf
 */
public Boolean getIsLeaf() {
    return isLeaf;
}

/**
 * @设置isLeaf的值
 * @param isLeaf the isLeaf to set
 */
public void setIsLeaf(Boolean isLeaf) {
    this.isLeaf = isLeaf;
}

/**
 * @取得expanded的值
 * @return the expanded
 */
public Boolean getExpanded() {
    return expanded;
}

/**
 * @设置expanded的值
 * @param expanded the expanded to set
 */
public void setExpanded(Boolean expanded) {
    this.expanded = expanded;
}

/**
 * @取得children的值
 * @return the children
 */
public List<Tree> getChildren() {
    return children;
}

/**
 * @设置children的值
 * @param children the children to set
 */
public void setChildren(List<Tree> children) {
    this.children = children;
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134

}

二、递归的实现类

public class TreeUtil {

/**
 * 格式化list为树形list
 * @param list 需要格式化的list
 * @param flag true 表示全部展开,其他 表示不展开
 * @param <T> 格式化的实体对象
 * @return 格式化之后的list
 */
public static <T extends Tree> List<T> formatTree(List<T> list, Boolean flag) {

    List<T> nodeList = new ArrayList<T>();  
    for(T node1 : list){  
        boolean mark = false;  
        for(T node2 : list){  
            if(node1.getParentKey()!=null && node1.getParentKey().equals(node2.getKey())){ 
                node2.setIsLeaf(false);
                mark = true;  
                if(node2.getChildren() == null) {
                    node2.setChildren(new ArrayList<Tree>());  
                }
                node2.getChildren().add(node1); 
                if (flag) {
                    //默认已经全部展开
                } else{
                    node2.setExpanded(false);
                }
                break;  
            }  
        }  
        if(!mark){  
            nodeList.add(node1);   
            if (flag) {
                //默认已经全部展开
            } else{
                node1.setExpanded(false);
            }
        }  
    }
    return nodeList;
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

}

注:需要递归的对应的实体信息,需继承Tree类

本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!