package com.atguigu.commonutils;
import com.atguigu.commonutils.entity.A;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
*
*
*/
public class TreeUtils {
/**
*根据关键字查询树形结构的结果
*
* @param keyWords
*/
public void getTreeList(
String keyWords){
if(!StringUtils.isEmpty(keyWords)){
//获取模糊查询子集
List<A> likeList = this.selectByName();
//获取全量数据结果集合
List<A> allList = this.selectAll();
Map<String, A> allMap = allList.stream().collect(Collectors.toMap(A::getId, Function.identity(), (f1, f2) -> f2));
Set<String> likeIdList = likeList.stream().map(A::getId).collect(Collectors.toSet());
//寻找需要查询数据的父节点
findAllPIdInfo(allMap,likeIdList,likeList);
//所有子集和子集的所有父节点的List集合(树结构不全)
List<A> adminTreeList = allList.stream().filter(i -> likeIdList.contains(i.getId())).collect(Collectors.toList());
//用删选出的所有可以作树的集合重新做树结构
setTreeContract(adminTreeList,keyWords);
//筛选最父节点返回(上一行函数做完树把子节点在集合中没删除)
List<A> resuleTree = adminTreeList.stream().filter(i -> "root".equals(i.getId())).collect(Collectors.toList());
}
}
/**
* 做树结构 并且把没有子集的节点children置空
* @param adminTreeList
* @param keyWords
*/
private void setTreeContract(List<A> adminTreeList, String keyWords) {
if(CollectionUtils.isEmpty(adminTreeList)){
return;
}
for (A a:adminTreeList){
a.getChildren().clear();
}
Map<String, A> adminMap = adminTreeList.stream().collect(Collectors.toMap(A::getId, Function.identity(), (f1, f2) -> f2));
for(A i :adminTreeList){
if(!StringUtils.isEmpty(i.getPId() ) && !"root".equals(i.getPId()) ){
A parent = adminMap.get(i.getPId());
//做树
List<A> children = parent.getChildren();
children.add(i);//把自己 加入到父节点的子集中
}
}
if(!StringUtils.isEmpty(keyWords)){
this.setChildrenNull(adminTreeList);
}
}
/**
* 把没有子集的节点children置空
* @param adminTreeList
*/
private void setChildrenNull(List<A> adminTreeList) {
List<A> nextlist = new ArrayList<>();//作为递归下一次入参
for (A a : adminTreeList){
if(!CollectionUtils.isEmpty(a.getChildren())){
nextlist.addAll(a.getChildren());//添加子集到递归下一次入参
}else {
a.setChildren(null);
}
}
if(!CollectionUtils.isEmpty(nextlist)){
setChildrenNull(nextlist);
}
}
/**
* 寻找所有服务节点id
* @param allMap
* @param likeIdList
* @param likeList
*/
private void findAllPIdInfo(Map<String, A> allMap , Set<String> likeIdList, List<A> likeList) {
List<A> newResultList = new ArrayList<>();
likeList.forEach(i->{
if(!StringUtils.isEmpty(i.getPId() ) && !"root".equals(i.getPId()) && !likeIdList.contains(i.getPId())){
likeIdList.add(i.getPId());//寻找所有的父节点Id
A parent = allMap.get(i.getPId());
newResultList.add(parent);
//做树
List<A> children = parent.getChildren();
children.add(i);//把自己加入到父节点的子集中
}
});
//递归寻找父节点
if(!CollectionUtils.isEmpty(newResultList)){
findAllPIdInfo(allMap,likeIdList,newResultList);
}
}
/**
* 获取模糊查询数据集合
* @return
*/
private List<A> selectByName(){
List<A> a = new ArrayList<>();
return a;
}
/**
* 获取全量数据集合
* @return
*/
private List<A> selectAll(){
List<A> a = new ArrayList<>();
return a;
}
}
根据关键字模糊查询,以树结构返回结构
最新推荐文章于 2022-10-28 17:32:13 发布