mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
添加forEachChild方法,putAllNode允许忽略key为null的值
This commit is contained in:
parent
04b6c3bcdc
commit
0ca143b0d0
@ -90,17 +90,39 @@ public interface ForestMap<K, V> extends Map<K, TreeEntry<K, V>> {
|
|||||||
* @param values 要添加的值
|
* @param values 要添加的值
|
||||||
* @param keyGenerator 从值中获取key的方法
|
* @param keyGenerator 从值中获取key的方法
|
||||||
* @param parentKeyGenerator 从值中获取父节点key的方法
|
* @param parentKeyGenerator 从值中获取父节点key的方法
|
||||||
|
* @param ignoreNullNode 是否获取到的key为null的子节点/父节点
|
||||||
*/
|
*/
|
||||||
default <C extends Collection<V>> void putAllNode(
|
default <C extends Collection<V>> void putAllNode(
|
||||||
C values, Function<V, K> keyGenerator, Function<V, K> parentKeyGenerator) {
|
C values, Function<V, K> keyGenerator, Function<V, K> parentKeyGenerator, boolean ignoreNullNode) {
|
||||||
if (CollUtil.isEmpty(values)) {
|
if (CollUtil.isEmpty(values)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
values.forEach(v -> {
|
values.forEach(v -> {
|
||||||
final K key = keyGenerator.apply(v);
|
final K key = keyGenerator.apply(v);
|
||||||
final K parentKey = parentKeyGenerator.apply(v);
|
final K parentKey = parentKeyGenerator.apply(v);
|
||||||
linkNodes(parentKey, key);
|
|
||||||
get(key).setValue(v);
|
// 不忽略keu为null节点
|
||||||
|
boolean hasKey = ObjectUtil.isNotNull(key);
|
||||||
|
boolean hasParentKey = ObjectUtil.isNotNull(parentKey);
|
||||||
|
if (!ignoreNullNode || (hasKey && hasParentKey)) {
|
||||||
|
linkNodes(parentKey, key);
|
||||||
|
get(key).setValue(v);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 父子节点的key都为null
|
||||||
|
if (!hasKey && !hasParentKey) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 父节点key为null
|
||||||
|
if (hasKey) {
|
||||||
|
putNode(key, v);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 子节点key为null
|
||||||
|
putNode(parentKey, null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,6 +89,8 @@ public interface TreeEntry<K, V> extends Map.Entry<K, V> {
|
|||||||
return ObjectUtil.isNotNull(getParent(key));
|
return ObjectUtil.isNotNull(getParent(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ===================== 子节点相关方法 =====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取以当前节点作为根节点的树结构,然后遍历所有节点
|
* 获取以当前节点作为根节点的树结构,然后遍历所有节点
|
||||||
*
|
*
|
||||||
@ -97,8 +99,6 @@ public interface TreeEntry<K, V> extends Map.Entry<K, V> {
|
|||||||
*/
|
*/
|
||||||
void forEachChild(boolean includeSelf, Consumer<TreeEntry<K, V>> nodeConsumer);
|
void forEachChild(boolean includeSelf, Consumer<TreeEntry<K, V>> nodeConsumer);
|
||||||
|
|
||||||
// ===================== 子节点相关方法 =====================
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前节点直接关联的子节点
|
* 获取当前节点直接关联的子节点
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user