ZtreeUtils.java
package pit.common.utils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import pit.common.web.response.Ztree;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class ZtreeUtils {
public static List getZreeList(List ztreeList) {
List parents = getParents(ztreeList);
// List parents = ztreeList.stream()
// .filter(f-> StringUtils.isEmpty(f.getPId()) || "null".equals(f.getPId()))
// .collect(Collectors.toList());
for (Ztree p : parents) {
p.setIsOpen(false);
List childPerms = getChildPerms(ztreeList, p.getId());
if(childPerms == null){
childPerms = new ArrayList<>();
}
p.setChildren(childPerms);
}
return parents;
}
private static List getParents(List treeList) {
if (treeList == null || treeList.size() < 1) {
return Collections.emptyList();
}
List ids = treeList.stream().map(i -> i.getId())
.filter(StrUtil::isNotEmpty).distinct()
.collect(Collectors.toList());
//pid在ids里面找不到的
return treeList.stream().filter(i -> !ids.contains(i.getPId()))
.collect(Collectors.toList());
}
public static List getChildPerms(List list, String parentId) {
parentId = null == parentId ? "" : parentId;
List returnList = new ArrayList<>();
for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
Ztree t = iterator.next();
// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
if (parentId.equals(t.getPId())) {
returnList.add(t);
recursionFn(list, t);
}
}
return returnList;
}
private static void recursionFn(List list, Ztree t) {
// 得到子节点列表
List childList = getChildList(list, t);
if(childList == null){
childList = new ArrayList<>();
}
t.setChildren(childList);
for (Ztree tChild : childList) {
if (hasChild(list, tChild)) {
// 判断是否有子节点
Iterator it = childList.iterator();
while (it.hasNext()) {
Ztree n = it.next();
recursionFn(list, n);
}
}
}
}
private static List getChildList(List list, Ztree t) {
List tlist = new ArrayList<>();
Iterator it = list.iterator();
while (it.hasNext()) {
Ztree n = it.next();
if (t.getId().equals(n.getPId())) {
tlist.add(n);
}
}
return tlist;
}
private static boolean hasChild(List list, Ztree t) {
for (Ztree ztree : list) {
if (t.getId().equals(ztree.getPId())) {
return true;
}
}
return false;
}
public static List getBuildTree(List list){
List trees = new ArrayList<>();
for(Ztree node:list){
if(StringUtils.isEmpty(node.getPId())){
trees.add(findChild(node,list));
}
}
return trees;
}
//解决上级id不为空也能筛选出来
public static List getBuildTreeNew(List list){
List trees = getParents(list);
for (Ztree node : trees){
findChild(node,list);
}
return trees;
}
private static Ztree findChild(Ztree node, List list){
for(Ztree n:list){
if(node.getId().equals(n.getPId())){
node.getChildren().add(findChild(n,list));
}
}
return node;
}
}
Ztree.java
package pit.common.web.response;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Data
public class Ztree implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String pId;
private String name;
private String title;
private String description;
private String type;
private Boolean checked = false;
private Boolean open = false;
private Boolean isOpen = true;
private Boolean nocheck = false;
private Boolean isEnable = true;
private Integer sort;
private List Children = new ArrayList<>();
private Integer count;
private Boolean isDisabled = true;
}
Test
public static void main(String[] args) {
List dataList = new ArrayList<>();
//调用 返回树结构
ZtreeUtils.getBuildTreeNew(initZtree(dataList));
}
public List initZtree(List list) {
List ztreeList = new ArrayList<>();
for (PitDdsDrawingCatalogEntity pitDdsDrawingCatalog : list) {
Ztree ztree = new Ztree();
ztree.setId(pitDdsDrawingCatalog.getDrawingCatalogId());
ztree.setPId(pitDdsDrawingCatalog.getUpDrawingCatalogId());
ztree.setName(pitDdsDrawingCatalog.getDrawingCatalogName());
ztreeList.add(ztree);
}
return ztreeList;
}