###結果以json格式輸出,可以用json線上解析,方便查看 package com.xintone.demo; import cn.hutool.json.JSONUtil; import lombok.Data; import org.springframework.util.Collecti ...
結果以json格式輸出,可以用json線上解析,方便查看
package com.xintone.demo;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Data
public class Recursion {
// 主鍵id
private Integer id;
// 父級id
private Integer parentId;
// 子集
private List<Recursion> children;
// 層級
private Integer level;
public Recursion(Integer id, Integer parentId) {
this.id = id;
this.parentId = parentId;
}
public static void main(String[] args) {
// 獲取測試數據
List<Recursion> recursions1 = getList();
// 獲取頂級父集
List<Recursion> parents = recursions1.stream().filter(item -> item.getParentId().equals(0)).collect(Collectors.toList());
// 設置層級
parents.forEach(item -> item.setLevel(0));
// 向下遞歸
downwardRecursion(parents, recursions1);
System.out.println("向下遞歸:" + JSONUtil.toJsonStr(parents));
System.out.println("-------------分割線-------------");
// 獲取測試數據
List<Recursion> recursions2 = getList();
// 獲取測試數據中所有的 parentId
List<Integer> parentIds = recursions2.stream().map(Recursion::getParentId).collect(Collectors.toList());
// 判斷 id 是否在 parentIds 中,不在則是最子級
List<Recursion> children = recursions2.stream().filter(item -> !parentIds.contains(item.getId())).collect(Collectors.toList());
// 向上遞歸
upwardRecursion(children, recursions2);
// 遞歸完從測試數據中篩選出最頂級
List<Recursion> tree = recursions2.stream().filter(item -> item.getParentId().equals(0)).collect(Collectors.toList());
System.out.println("向上遞歸:" + JSONUtil.toJsonStr(tree));
}
private static void upwardRecursion(List<Recursion> children, List<Recursion> all) {
// 遍歷子集
children.forEach(child -> {
// 獲取該子級的父級
Recursion parent = all.stream().filter(item -> child.getParentId().equals(item.getId())).findFirst().orElse(null);
// 判斷父級是否為空,如果為空則是最頂級
if (parent != null) {
// 判斷父級的子集是否為空
if (parent.getChildren() == null) {
// 新建一個集合將子級添加到父級的子集中
parent.setChildren(new ArrayList<Recursion>() {{
add(child);
}});
// 判斷該子級是否在父級的子集中存在
// 因為在遍歷子級時,會出現多個子級的父級相同,如果不加判斷會導致數據重覆
} else if (!parent.getChildren().contains(child)) {
// 將該子級添加到父級的子集中
parent.getChildren().add(child);
}
// 將父級添加到集合併向上遞歸
upwardRecursion(new ArrayList<Recursion>() {{
add(parent);
}}, all);
child.setLevel(parent.getLevel() + 1);
} else {
child.setLevel(0);
}
});
}
private static void downwardRecursion(List<Recursion> parents, List<Recursion> all) {
// 遍歷父集
parents.forEach(recursion -> {
// 獲取該父級的子集
List<Recursion> children = all.stream().filter(item -> item.getParentId().equals(recursion.getId())).collect(Collectors.toList());
// 判斷子集是否為空
if (!CollectionUtils.isEmpty(children)) {
// 設置子集的層級
children.forEach(item -> item.setLevel(recursion.getLevel() + 1));
// 將子集添加到該父級的子集去
recursion.setChildren(children);
// 向下遞歸
downwardRecursion(children, all);
}
});
}
private static List<Recursion> getList() {
List<Recursion> list = new ArrayList<>();
list.add(new Recursion(1, 0));
list.add(new Recursion(2, 0));
list.add(new Recursion(3, 0));
list.add(new Recursion(4, 1));
list.add(new Recursion(5, 1));
list.add(new Recursion(6, 2));
list.add(new Recursion(7, 2));
list.add(new Recursion(8, 3));
list.add(new Recursion(9, 3));
list.add(new Recursion(10, 4));
list.add(new Recursion(11, 4));
list.add(new Recursion(12, 5));
list.add(new Recursion(13, 5));
list.add(new Recursion(14, 6));
list.add(new Recursion(15, 6));
list.add(new Recursion(16, 7));
list.add(new Recursion(17, 7));
list.add(new Recursion(18, 8));
list.add(new Recursion(19, 8));
list.add(new Recursion(20, 9));
list.add(new Recursion(21, 9));
return list;
}
}