增加行政区划树接口

This commit is contained in:
lxj
2025-06-19 18:26:41 +08:00
parent fabb2d8abc
commit d6903acf64
4 changed files with 11 additions and 11 deletions

View File

@@ -7,9 +7,9 @@ import java.util.List;
* @param <E>
*/
public interface TreeEntity<E, T> {
public T getId();
T getId();
public T getParentId();
T getParentId();
public void setChildren(List<E> children);
void setChildren(List<E> children);
}

View File

@@ -21,17 +21,17 @@ public class TreeUtils {
* @param rootCode 根节点编码
* @return 构建树状
*/
public static <T extends TreeEntity> List<T> getTreeList(List<T> entityList, Object rootCode) {
public static <T extends TreeEntity<T, ?>> 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);
groupData.computeIfAbsent(entity.getParentId(), (k) -> new ArrayList<>()).add(entity);
}
// 第二次循环 生成树结构
for (TreeEntity entity : entityList) {
for (TreeEntity<T, ?> entity : entityList) {
List<T> children = groupData.get(entity.getId());
if (children != null) {
entity.setChildren(children);