!699 ForestMap添加getNodeValue方法

Merge pull request !699 from Createsequence/feat-node-value
This commit is contained in:
Looly 2022-07-16 00:39:30 +00:00 committed by Gitee
commit c2ebdb4476
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 20 additions and 0 deletions

View File

@ -275,6 +275,18 @@ public interface ForestMap<K, V> extends Map<K, TreeEntry<K, V>> {
.orElse(false);
}
/**
* 获取指定节点的值
*
* @param key 节点的key
* @return 节点值若节点不存在或节点值为null都将返回null
*/
default V getNodeValue(K key) {
return Opt.ofNullable(get(key))
.map(TreeEntry::getValue)
.get();
}
// ===================== 子节点相关方法 =====================
/**

View File

@ -142,6 +142,14 @@ public class LinkedForestMapTest {
Assert.assertFalse(c.hasChildren());
}
@Test
public void getNodeValueTest() {
final ForestMap<String, String> map = new LinkedForestMap<>(false);
map.putNode("a", "aaa");
Assert.assertEquals("aaa", map.getNodeValue("a"));
Assert.assertNull(map.getNodeValue("b"));
}
@Test
public void putAllNodeTest() {
final ForestMap<String, Map<String, String>> map = new LinkedForestMap<>(false);