增加行政区划树接口
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package org.dromara.common.core.domain;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 通用tree构建工具类
|
||||
* @param <E>
|
||||
*/
|
||||
public interface TreeEntity<E, T> {
|
||||
public T getId();
|
||||
|
||||
public T getParentId();
|
||||
|
||||
public void setChildren(List<E> children);
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
package org.dromara.common.core.utils;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import org.dromara.common.core.domain.TreeEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* *解析树形数据工具类
|
||||
*/
|
||||
public class TreeUtils {
|
||||
|
||||
/**
|
||||
* 将列表数据构建成树结构
|
||||
*
|
||||
* @param entityList 列表数据
|
||||
* @param rootCode 根节点编码
|
||||
* @return 构建树状
|
||||
*/
|
||||
public static <T extends TreeEntity> List<T> getTreeList(List<T> entityList, Object rootCode) {
|
||||
if (CollectionUtil.isEmpty(entityList)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
//第一次循环 数据分组
|
||||
Map<Object, List<T>> groupData = new HashMap<>();
|
||||
for (T entity : entityList) {
|
||||
groupData.computeIfAbsent(entity.getParentId(), (k) -> new ArrayList<T>()).add(entity);
|
||||
}
|
||||
// 第二次循环 生成树结构
|
||||
for (TreeEntity entity : entityList) {
|
||||
List<T> children = groupData.get(entity.getId());
|
||||
if (children != null) {
|
||||
entity.setChildren(children);
|
||||
} else {
|
||||
entity.setChildren(new ArrayList<>());
|
||||
}
|
||||
}
|
||||
return groupData.get(rootCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user