目录
工具类
/**
* @Auther: jhwang
* @Date: 2022/8/27 13:54
* @Description:
*/
public class TreeUtils<T,M> {
private T treeOne;
public TreeUtils(T treeOne){
this.treeOne = treeOne;
}
/**
* 构架树并返回
* @param list 集合
* @param treeData 构建tree关键节点需要用到的数据
* @return
*/
public List<M> structureTreeData(List<M> list,TreeData<T,M> treeData){
//不用递归,用分组排序的方法
//return this.structureTree(this.treeOne,list,treeData);
return this.groupSort(list,treeData);
};
/**
* 分组排序的方法效率最高
* @param list
* @param treeData
* @return
*/
private List<M> groupSort(List<M> list,TreeData<T,M> treeData){
//分组排序的方法
ArrayList<M> ms = new ArrayList<>();
Map<T, List<M>> map = list.stream().collect(Collectors.groupingBy(item ->
treeData.getPid(item)
));
map.forEach((pCode, value) -> {
if (pCode.equals(this.treeOne)) {
ms.addAll(value);
}
//遍历list通过code查出当前list设置children
value.forEach(item -> {
treeData.setChilds(item,map.get(treeData.getId(item)));
});
});
return ms;
}
/**
* 构建树形结构
* @param pid 父系节点id
* @param list 总的集合
* @param treeData
* @return
*/
private List<M> structureTree(T pid,List<M> list,TreeData<T,M> treeData){
List<M> outList = getList(pid,list,treeData);
outList.forEach(item->{
this.setChilds(item,treeData);//防止null指针
treeData.setChilds(item,structureTree(treeData.getId(item),list,treeData));
});
return outList;
}
/**
* 根据id设置子类并返回剩余的集合,并删除已经设置的节点
* @param pid
* @param list
* @param treeData
* @return
*/
private List<M> getList(T pid, List<M> list,TreeData<T, M> treeData) {
Iterator<M> iterator = list.iterator();
List<M> datas = new ArrayList<>();
while (iterator.hasNext()) {
M next = iterator.next();
if(this.judgeEquals(pid,next,treeData)){
//如果是主节点
datas.add(next);
iterator.remove();
}
}
return datas;
}
/**
* 判断id和pid是否相同
* @param pid
* @param m
* @param treeData
* @return
*/
private boolean judgeEquals(T pid,M m,TreeData<T, M> treeData){
if(pid instanceof String || pid instanceof Integer){
return pid.equals(treeData.getPid(m));
}
throw new RuntimeException(TreeUtils.class+"什么鬼主键类型不识别");
}
private void setChilds(M m ,TreeData<T,M> treeData){
if(treeData.getChilds(m) == null){
treeData.setChilds(m,new ArrayList<M>());
}
}
/**
* 将树形结构转list
* @return
*/
public List<M> treeToList(List<M> treeList,TreeData<T,M> treeData){
return flattenTreeToList(treeList,treeData,new ArrayList<M>());
}
public List<M> flattenTreeToList(List<M> treeList,TreeData<T,M> treeData,List<M> result){
if(treeList != null && treeList.size()>0){
treeList.forEach(item->{
result.add(item);
flattenTreeToList(treeData.getChilds(item),treeData,result);
});
}
return result;
}
}
使用例子
List<CityData> cityData = new TreeUtils<String, CityData>("0")
.structureTreeData(cityDataList, new TreeData<String, CityData>() {
@Override
protected String getId(CityData cityData) {
return cityData.getCode();
}
@Override
protected String getPid(CityData cityData) {
return cityData.getPcode();
}
@Override
protected List<CityData> getChilds(CityData cityData) {
return cityData.getChildren();
}
@Override
protected void setChilds(CityData cityData, List<CityData> list) {
cityData.setChildren(list);
}
});