java递归树状json

一、建立递归树的实体类

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;
}

}

二、递归的实现类

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;
}

}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值