java 树形对象工具类封装

时间:2021-03-08 15:37:48
基础的树形对象:
import java.io.Serializable;
import java.util.List;

/**
* treegrid树形表格基础对象,后续的该类型的对象均继承该对象
*
*/
public class BaseTreeGrid implements Serializable{

/**
*
*/
private static final long serialVersionUID = -9189631784252440402L;

public String id;//节点id

public String parentId;//节点父id

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

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

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

public List<BaseTreeGrid> children;//孩子节点


public BaseTreeGrid() {

}

public BaseTreeGrid(String id, String parentId) {
this.id=id;
this.parentId=parentId;
}

public String getId() {
return id;
}

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

public String getParentId() {
return parentId;
}

public void setParentId(String parentId) {
this.parentId = parentId;
}

public String getIconCls() {
return iconCls;
}

public void setIconCls(String iconCls) {
this.iconCls = iconCls;
}

public Boolean getLeaf() {
return leaf;
}

public void setLeaf(Boolean leaf) {
this.leaf = leaf;
}

public Boolean getExpanded() {
return expanded;
}

public void setExpanded(Boolean expanded) {
this.expanded = expanded;
}

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

public void setChildren(List<BaseTreeGrid> children) {
this.children = children;
}
}
import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSONArray;import com.eshore.common.ext.BaseTreeGrid;/** * 将记录list转化为树形list * 基于BaseTreeGid类的转换 * */public class TreeUtils {/** * 格式化list为树形list * @param list * @param falg true 表示全部展开,其他 表示不展开 * @return */public static <T extends BaseTreeGrid> 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.getParentId()!=null && node1.getParentId().equals(node2.getId())){         node2.setLeaf(false);            mark = true;              if(node2.getChildren() == null) {            node2.setChildren(new ArrayList<BaseTreeGrid>());              }            node2.getChildren().add(node1);             if (flag) {            //默认已经全部展开            } else{            node2.setExpanded(false);            }            break;          }      }      if(!mark){          nodeList.add(node1);           if (flag) {            //默认已经全部展开            } else{            node1.setExpanded(false);            }    }  }return nodeList;}public static void main(String[] args) {List<BaseTreeGrid> list = new ArrayList<BaseTreeGrid>();BaseTreeGrid root1 = new BaseTreeGrid();root1.setId("1");BaseTreeGrid child1 = new BaseTreeGrid();child1.setId("11");child1.setParentId("1");BaseTreeGrid child11 = new BaseTreeGrid();child11.setId("111");child11.setParentId("11");BaseTreeGrid root2 = new BaseTreeGrid();root2.setId("2");BaseTreeGrid child2 = new BaseTreeGrid();child2.setId("21");child2.setParentId("2");list.add(root1);list.add(child1);list.add(child11);list.add(root2);list.add(child2);List<BaseTreeGrid> treelist = formatTree(list, false);String json = JSONArray.toJSONString(treelist);System.out.println(json);}}