如果键匹配,Java将`HashMap`值附加到现有的HashMap中 [英] Java append `HashMap` values to existing HashMap if key matches
问题描述
我下面印有下面的HashMap(to.String())。
I have below HashMap(to.String()) printed below.
HashMap< String,HashMap< String,HashMap< String,整数>>> abc = new HashMap<>();
HashMap abc = {disabled = {account = {testConfiguration = 1,iterate = 1}}}
我要附加 {group = {iterate = 1}}
到现有地图,如果键禁用
匹配。
I want to append {group={iterate=1}}
to existing map if key disabled
matches.
最后,我的地图应如下图所示,我该如何实现?
Finally my map should look like below, how can I achieve it?
HashMap abc = {disabled = {account = {testConfiguration = 1,iterate = 1},{group = { iterate = 1}}}
推荐答案
我认为您想要这样:
abc.computeIfPresent("disabled", (k,v) -> {
v.put("group", yourValue);
return v;
});
或简单地:
if (abc.containsKey("disabled")) {
abc.get("disabled").put("group", yourValue);
}
我个人更喜欢第一种方法,因为它更快一些并且可以正常使用并发地图。
I personally prefer the first approach, since it's a bit faster and works properly with concurrent maps.
这篇关于如果键匹配,Java将`HashMap`值附加到现有的HashMap中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!