我在构建一个hashmap时遇到了一些麻烦但是我花了很多时间而且我对这类对象没有很多经验,我的问题是在构建下一个hashmap时:
Map<String, Map<String, Map<String, Object>>> map = new HashMap<String, Map<String, Map<String, Object>>>();
Map<String, Map<String, Object>> map1 = new HashMap<String, Map<String, Object>>();
Map<String, Object> map2 = new HashMap<String, Object>();
Map<String, Object> map3 = new HashMap<String, Object>();
map2.put("one",1);
map1.put("two", map2);
map.put("cero", map1);
System.out.println(map);
输出结果为:
{cero={two={one=1}}}
但现在我想在percent
关键级别添加另一个键10
,字符串值为cero
,如:
{percent=10,cero={two={one=1}}}
我尝试过类似的事情:
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("percent", "10");
map.get("cero").putAll(map3);
putAll
方法中存在错误,因为java需要Map<String, Map<String, Object>>
种对象,但我只需要添加percent
属性。希望我清楚我的问题,你们可以帮助我,谢谢。
答案 0 :(得分:0)
我们可以尝试将map1添加到map3
Map<String, Map<String, Map<String, Object>>> map = new HashMap<String,
Map<String, Map<String, Object>>>();
Map<String, Map<String, Object>> map1 = new HashMap<String, Map<String, Object>>
();
Map<String, Object> map2 = new HashMap<String, Object>();
Map<String, Object> map3 = new HashMap<String, Object>();
map2.put("one",1);
map1.put("two", map2);
map3.put("percent", "10");
map3.put("cero",map1);
System.out.println(map3);
这在map3中给出以下结构 {percent = 10,cero = {two = {one = 1}}}