From 80bdac99d9dd4b3932cde87343cf682c81e85b5b Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 16 Aug 2023 10:31:50 +0800 Subject: [PATCH] =?UTF-8?q?TreeUtil=E5=A2=9E=E5=8A=A0getParentsId=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- .../cn/hutool/core/lang/tree/TreeUtil.java | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 968c289f7..6c1cdbeac 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.22(2023-08-15) +# 5.8.22(2023-08-16) ### 🐣新特性 * 【core 】 NumberUtil.nullToZero增加重载(issue#I7PPD2@Gitee) @@ -12,6 +12,7 @@ * 【http 】 优化HttpUtil.urlWithForm方法(pr#1052@Gitee) * 【http 】 优化HttpUtil.urlWithForm方法(pr#1052@Gitee) * 【cron 】 优化PatternParser支持年的步进(issue#I7SMP7@Gitee) +* 【core 】 TreeUtil增加getParentsId方法(issue#I7TDCF@Gitee) ### 🐞Bug修复 * 【core 】 修复NumberUtil.toBigDecimal转换科学计数法问题(issue#3241@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/tree/TreeUtil.java b/hutool-core/src/main/java/cn/hutool/core/lang/tree/TreeUtil.java index 710497e03..7577e496e 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/tree/TreeUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/tree/TreeUtil.java @@ -230,6 +230,42 @@ public class TreeUtil { return result; } + /** + * 获取所有父节点ID列表 + * + *

+ * 比如有个人在研发1部,他上面有研发部,接着上面有技术中心
+ * 返回结果就是:[研发部, 技术中心] + * + * @param 节点ID类型 + * @param node 节点 + * @param includeCurrentNode 是否包含当前节点的名称 + * @return 所有父节点ID列表,node为null返回空List + * @since 5.8.22 + */ + public static List getParentsId(Tree node, boolean includeCurrentNode) { + final List result = new ArrayList<>(); + if (null == node) { + return result; + } + + if (includeCurrentNode) { + result.add(node.getId()); + } + + Tree parent = node.getParent(); + T id; + while (null != parent) { + id = parent.getId(); + parent = parent.getParent(); + if(null != id || null != parent){ + // issue#I795IN,根节点的null不加入 + result.add(id); + } + } + return result; + } + /** * 创建空Tree的节点 *