[英]Removing element of a Map inside a TreeMap
I have this structure 我有这个结构
TreeMap<String,Map<String,String>> map;
And i want to remove one of the key-value pair inside the Map found by a key. 而且我想删除由键找到的Map内的键值对之一。 I tried:
我试过了:
map.get(key).remove(key2);
But it throws a NullPointerException. 但是它抛出NullPointerException。 To find the problem i tried:
为了找到我尝试的问题:
Map<String,String> aux = map.get(key);
aux.remove(key2);
and saw that the map.get just returns null. 并看到map.get仅返回null。
Any solution? 有什么办法吗? Thank you in advance!
先感谢您!
You could check if the outer key (in this case key
) is present in the outer map and invoke the second get
only if that is true. 您可以检查外部映射中是否存在外部键(在本例中为
key
),并仅在为true时才调用第二个get
。
Map<String,String> outerKey = map.get(key);
if(outerKey != null) {
outerKey.remove(key2)
}
You get the NullPointerException
because the key key
is not present in the outer map (or is mapped to a null value), so map.get(key)
returns null. 之所以会得到
NullPointerException
是因为外部映射中不存在key
(或键映射为null值),因此map.get(key)
返回null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.