改为泛型
对象
@Data
public class ListTree <T> {
/**
* ID
*/
private Long id;
/**
* 父ID
*/
private Long pid;
/**
* 名称
*/
private String title;
/**
* 数量
*/
private Integer num;
/**
* isFather 是否为父节点
*/
private Boolean disabled;
List<T> children;
}
具体方法
public class ListToTree {
/**
* 返回全部树
* @param srcList
* @return
*/
public static <T extends ListTree> List<T> toTree(List<T> srcList) {
if (srcList == null) {
return null;
}
List<T> resultList = new ArrayList<>();
List<T> list = srcList.stream().filter(listTree -> "0".equals(listTree.getPid().toString())).collect(Collectors.toList());
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
for (int i = 0; i < list.size(); i++) {
setChildren(list.get(i), pidMap);
resultList.add(list.get(i));
}
return resultList;
}
/**
* 返回当前节点下的树
* @param srcList
* @param id
* @return
*/
public static <T extends ListTree> List<T> toTreeById(List<T> srcList,String id) {
if (srcList == null) {
return null;
}
List<T> resultList = new ArrayList<>();
List<T> list = srcList.stream().filter(listTree -> id.equals(listTree.getId().toString())).collect(Collectors.toList());
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
resultList.add(list.get(0));
setChildren(list.get(0), pidMap);
return resultList;
}
private static <T extends ListTree> void setChildren(T dto, Map<Long, List<T>> pidMap) {
List<T> childrenList = pidMap.get(dto.getId());
if (childrenList == null || childrenList.isEmpty()) {
dto.setChildren(Collections.EMPTY_LIST);
return;
}
dto.setChildren(childrenList);
for (T child : childrenList) {
setChildren(child, pidMap);
}
}
/**
* 返回当前节点下的树(不包括本节点)
* @param srcList
* @param id
* @return
*/
public static <T extends ListTree> List<T> toTreeByNoId(List<T> srcList,String id) {
if (srcList == null) {
return null;
}
List<ListTree> list = srcList.stream().filter(listTree -> id.equals(listTree.getId().toString())).collect(Collectors.toList());
Map<Long, List<ListTree>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(ListTree::getPid, Collectors.toList()));
ListTree listTree = list.get(0);
setChildren(list.get(0), pidMap);
return listTree.getChildren();
}
/**
* 返回当前节点下的list
* @param srcList
* @param id
* @return
*/
public static <T extends ListTree> List<T> toListById(List<T> srcList,String id) {
if (srcList == null) {
return null;
}
List<T> resultList = new ArrayList<>();
List<T> list = srcList.stream().filter(listTree -> id.equals(listTree.getId().toString())).collect(Collectors.toList());
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
setChildrenList(list.get(0), pidMap,resultList);
return resultList;
}
/**
* 返回当前节点下的list(包含本级节点)
* @param srcList
* @param id
* @return
*/
public static <T extends ListTree> List<T> toListInId(List<T> srcList,String id) {
if (srcList == null) {
return null;
}
List<T> resultList = new ArrayList<>();
List<T> list = srcList.stream().filter(listTree -> id.equals(listTree.getId().toString())).collect(Collectors.toList());
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
resultList.add(list.get(0));
setChildrenList(list.get(0), pidMap,resultList);
return resultList;
}
private static <T extends ListTree> void setChildrenList(T dto, Map<Long, List<T>> pidMap,List<T> resultList) {
List<T> childrenList = pidMap.get(dto.getId());
if (childrenList == null || childrenList.isEmpty()) {
dto.setChildren(Collections.EMPTY_LIST);
return;
}
for (T child : childrenList) {
resultList.add(child);
setChildrenList(child, pidMap,resultList);
}
}
/**
* 本级及上级id
* @param srcList
* @param id
* @return
*/
public static <T extends ListTree> List<Long> idSuperId(List<T> srcList,String id) {
if (srcList == null) {
return null;
}
List<ListTree> list = srcList.stream().filter(listTree -> id.equals(listTree.getId().toString())).collect(Collectors.toList());
List<Long> resultList = new ArrayList<>();
Map<Long, List<ListTree>> idMap = srcList.stream()
.collect(Collectors.groupingBy(ListTree::getId, Collectors.toList()));
resultList.add(list.get(0).getId());
setSuper(resultList,list.get(0), idMap);
return resultList;
}
private static <T extends ListTree> void setSuper(List<Long> resultList,T dto, Map<Long, List<T>> idMap) {
if ("0".equals(dto.getPid().toString())) {
return;
}
List<T> fatherList = idMap.get(dto.getPid());
for (T father : fatherList) {
resultList.add(father.getId());
setSuper(resultList,father, idMap);
}
}
/**
* 本级及下级id
* @param srcList
* @param id
* @return
*/
public static <T extends ListTree> List<Long> idSubId(List<T> srcList,String id) {
if (srcList == null||"".equals(id)) {
return null;
}
List<T> list = srcList.stream().filter(listTree -> id.equals(listTree.getId().toString())).collect(Collectors.toList());
List<Long> resultList = new ArrayList<>();
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
resultList.add(list.get(0).getId());
setSub(resultList,list.get(0), pidMap);
return resultList;
}
/**
* 本级的下级id
* @param srcList
* @param id
* @return
*/
public static <T extends ListTree> List<Long> idSubIdNo(List<T> srcList,String id) {
if (srcList == null||"".equals(id)) {
return null;
}
List<T> list = srcList.stream().filter(listTree -> id.equals(listTree.getId().toString())).collect(Collectors.toList());
List<Long> resultList = new ArrayList<>();
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
setSub(resultList,list.get(0), pidMap);
return resultList;
}
private static <T extends ListTree> void setSub(List<Long> resultList,T dto, Map<Long, List<T>> pidMap) {
List<T> childrenList = pidMap.get(dto.getId());
if (childrenList == null || childrenList.isEmpty()) {
dto.setChildren(Collections.EMPTY_LIST);
return;
}
for (T child : childrenList) {
resultList.add(child.getId());
setSub(resultList,child, pidMap);
}
}
/**
* 返回全部树 (父节点不能选中)
* @param srcList
* @return
*/
public static <T extends ListTree> List<T> toTreeDisFa(List<T> srcList) {
if (srcList == null) {
return null;
}
List<T> resultList = new ArrayList<>();
List<T> list = srcList.stream().filter(listTree -> "0".equals(listTree.getPid().toString())).collect(Collectors.toList());
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
for (int i = 0; i < list.size(); i++) {
setChildren(list.get(i), pidMap);
resultList.add(list.get(i));
}
for (T tree : resultList) {
disable(tree);
}
return resultList;
}
/**
* 返回当前节点下的树(不包括本节点,父节点不能选中)
* @param srcList
* @param id
* @return
*/
public static <T extends ListTree> List<T> toTreeByNoIdDisFa(List<T> srcList,String id) {
if (srcList == null) {
return null;
}
List<T> list = srcList.stream().filter(listTree -> id.equals(listTree.getId().toString())).collect(Collectors.toList());
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
T listTree = list.get(0);
setChildren(list.get(0), pidMap);
List<T> children = listTree.getChildren();
for (T tree : children) {
disable(tree);
}
return children;
}
private static <T extends ListTree> void disable(T children) {
List<T> treeList = children.getChildren();
if (treeList == null || treeList.isEmpty()) {
children.setDisabled(false);
return;
} else {
children.setDisabled(true);
for (T tree : treeList) {
disable(tree);
}
}
}
/**
* 返回全部树及节点下的节点数量
* @param srcList
* @return
*/
public static <T extends ListTree> List<T> toNumTree(List<T> srcList) {
if (srcList == null) {
return null;
}
List<T> resultList = new ArrayList<>();
List<T> list = srcList.stream().filter(listTree -> "0".equals(listTree.getPid().toString())).collect(Collectors.toList());
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
for (int i = 0; i < list.size(); i++) {
setNumChildren(list.get(0), pidMap,srcList);
resultList.add(list.get(i));
}
return resultList;
}
/**
* 返回当前节点下的树及节点下的节点数量
* @param srcList
* @param id
* @return
*/
public static <T extends ListTree> List<T> toNumTreeById(List<T> srcList,String id) {
if (srcList == null) {
return null;
}
List<T> resultList = new ArrayList<>();
List<T> list = srcList.stream().filter(listTree -> id.equals(listTree.getId().toString())).collect(Collectors.toList());
Map<Long, List<T>> pidMap = srcList.stream()
.collect(Collectors.groupingBy(T::getPid, Collectors.toList()));
resultList.add(list.get(0));
setNumChildren(list.get(0), pidMap,srcList);
return resultList;
}
private static <T extends ListTree> void setNumChildren(T dto, Map<Long, List<T>> pidMap,List<T> srcList) {
List<T> childrenList = pidMap.get(dto.getId());
//计算本级下的标记数
List<Long> list = idSubIdNo(srcList, dto.getId() + "");
dto.setNum(list.size());
if (childrenList == null || childrenList.isEmpty()) {
dto.setChildren(Collections.EMPTY_LIST);
return;
}
dto.setChildren(childrenList);
for (T child : childrenList) {
setChildren(child, pidMap);
}
}
}