我有一个 Map of Maps
看起来像这样:
Map<String, Map<String, Object>> data;
我想更新内图。
我应该在每次更新内部 map 时执行以下操作,还是有任何其他直接/更好的方法来完成它?
Map<String, Object> innerMap = data.get("outer_key");
innerMap.put("inner_key", value);
data.put("outer_key", innerMap);
最佳答案
我想你可能有错别字,我假设 inner
实际上是 innerMap
。我可能会稍微更改它以说明外部 map 中的 null
值:
Map<String, Object> innerMap = data.get("outer_key");
// Added this if statement to handle null values in the outer map.
if (innerMap == null) {
innerMap = new HashMap<String, Object>();
data.put("outer_key", innerMap);
}
innerMap.put("inner_key", value);
// data.put("outer_key", innerMap); <-- This is not needed, innerMap is already a value in
// the outer map.
关于java - 如何更新 map 中的 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9069906/