mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
TreeUtil增加build方法,可以构建Bean的树结构
This commit is contained in:
parent
af67650742
commit
ee0b68dbb6
@ -2,9 +2,11 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.31(2024-08-09)
|
# 5.8.31(2024-08-11)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
|
* 【core 】 TreeUtil增加build方法,可以构建Bean的树结构(pr#3692@Github)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【extra 】 修复JakartaMailUtil引用javax的问题
|
* 【extra 】 修复JakartaMailUtil引用javax的问题
|
||||||
|
|
||||||
|
@ -291,9 +291,12 @@ public class TreeUtil {
|
|||||||
* @param <E> 节点类型
|
* @param <E> 节点类型
|
||||||
* @return List
|
* @return List
|
||||||
*/
|
*/
|
||||||
public static <T, E> List<E> build(List<E> nodes, T rootId, Function<E, T> idFunc, Function<E, T> parentIdFunc, BiConsumer<E, List<E>> setChildFunc) {
|
public static <T, E> List<E> build(List<E> nodes, T rootId,
|
||||||
|
Function<E, T> idFunc,
|
||||||
|
Function<E, T> parentIdFunc,
|
||||||
|
BiConsumer<E, List<E>> setChildFunc) {
|
||||||
List<E> rootList = nodes.stream().filter(tree -> parentIdFunc.apply(tree).equals(rootId)).collect(Collectors.toList());
|
List<E> rootList = nodes.stream().filter(tree -> parentIdFunc.apply(tree).equals(rootId)).collect(Collectors.toList());
|
||||||
Map<T, T> filterOperated = new HashMap<>(rootList.size() + nodes.size());
|
Set<T> filterOperated = new HashSet<>(rootList.size() + nodes.size());
|
||||||
//对每个根节点都封装它的孩子节点
|
//对每个根节点都封装它的孩子节点
|
||||||
rootList.forEach(root -> setChildren(root, nodes, filterOperated, idFunc, parentIdFunc, setChildFunc));
|
rootList.forEach(root -> setChildren(root, nodes, filterOperated, idFunc, parentIdFunc, setChildFunc));
|
||||||
return rootList;
|
return rootList;
|
||||||
@ -311,15 +314,15 @@ public class TreeUtil {
|
|||||||
* @param <T> 节点ID类型
|
* @param <T> 节点ID类型
|
||||||
* @param <E> 节点类型
|
* @param <E> 节点类型
|
||||||
*/
|
*/
|
||||||
private static <T, E> void setChildren(E root, List<E> nodes, Map<T, T> filterOperated, Function<E, T> idFunc, Function<E, T> parentIdFunc, BiConsumer<E, List<E>> setChildFunc) {
|
private static <T, E> void setChildren(E root, List<E> nodes, Set<T> filterOperated, Function<E, T> idFunc, Function<E, T> parentIdFunc, BiConsumer<E, List<E>> setChildFunc) {
|
||||||
List<E> children = new ArrayList<>();
|
List<E> children = new ArrayList<>();
|
||||||
nodes.stream()
|
nodes.stream()
|
||||||
//过滤出未操作过的节点
|
//过滤出未操作过的节点
|
||||||
.filter(body -> !filterOperated.containsKey(idFunc.apply(body)))
|
.filter(body -> !filterOperated.contains(idFunc.apply(body)))
|
||||||
//过滤出孩子节点
|
//过滤出孩子节点
|
||||||
.filter(body -> Objects.equals(idFunc.apply(root), parentIdFunc.apply(body)))
|
.filter(body -> Objects.equals(idFunc.apply(root), parentIdFunc.apply(body)))
|
||||||
.forEach(body -> {
|
.forEach(body -> {
|
||||||
filterOperated.put(idFunc.apply(body), idFunc.apply(root));
|
filterOperated.add(idFunc.apply(body));
|
||||||
children.add(body);
|
children.add(body);
|
||||||
//递归 对每个孩子节点执行同样操作
|
//递归 对每个孩子节点执行同样操作
|
||||||
setChildren(body, nodes, filterOperated, idFunc, parentIdFunc, setChildFunc);
|
setChildren(body, nodes, filterOperated, idFunc, parentIdFunc, setChildFunc);
|
||||||
|
Loading…
Reference in New Issue
Block a user