mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
TreeUtil增加getParentsId方法
This commit is contained in:
parent
1f66719187
commit
b9b539f0de
@ -243,6 +243,42 @@ public class TreeUtil {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有父节点ID列表
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 比如有个人在研发1部,他上面有研发部,接着上面有技术中心<br>
|
||||||
|
* 返回结果就是:[研发部, 技术中心]
|
||||||
|
*
|
||||||
|
* @param <T> 节点ID类型
|
||||||
|
* @param node 节点
|
||||||
|
* @param includeCurrentNode 是否包含当前节点的名称
|
||||||
|
* @return 所有父节点ID列表,node为null返回空List
|
||||||
|
* @since 5.8.22
|
||||||
|
*/
|
||||||
|
public static <T> List<T> getParentsId(MapTree<T> node, boolean includeCurrentNode) {
|
||||||
|
final List<T> result = new ArrayList<>();
|
||||||
|
if (null == node) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (includeCurrentNode) {
|
||||||
|
result.add(node.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
MapTree<T> 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的节点
|
* 创建空Tree的节点
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user