如何在java中制作嵌套HashMap

如何在java中制作嵌套HashMap,java,collections,hashmap,Java,Collections,Hashmap,我正在尝试将匿名hashmap放入另一个hashmap:- Map<String, Object> requestBody=new HashMap<String, Object>(); requestBody.put("UPSSecurity", new HashMap<String, Object>().put("username","rohan")); System.out.println(requestBody); 请使用此方法定义嵌套哈希映射

我正在尝试将匿名hashmap放入另一个hashmap:-

Map<String, Object> requestBody=new HashMap<String, Object>();
requestBody.put("UPSSecurity", new HashMap<String, Object>().put("username","rohan"));
System.out.println(requestBody);

请使用此方法定义嵌套哈希映射

    Map<String, Object> requestBody=new HashMap<String, Object>();
    Map<String,Object> userdetails=new HashMap<String, Object>();
    userdetails.put("username","rohan");
    requestBody.put("UPSSecurity",userdetails );
    System.out.println(requestBody);
Map requestBody=new HashMap();
Map userdetails=newhashmap();
userdetails.put(“用户名”、“rohan”);
requestBody.put(“upsecurity”,userdetails);
System.out.println(请求主体);
输出:

{upsecurity={username=rohan}}


你也可以这样做

Map<String, Object> requestBody=new HashMap<String, Object>();
requestBody.put("UPSSecurity", new HashMap<String, Object>());
requestBody.get("UPSSecurity").put("username","rohan");
Map requestBody=new HashMap();
put(“upsecurity”,newhashmap());
requestBody.get(“upsecurity”).put(“用户名”、“rohan”);

请参阅:您正在调用
put
,它将返回该键处的上一个元素。由于映射是空的,即
null
。因此,如果有10个嵌套的hashmap,那么我必须创建10个本地hashmapshow来提取UpsSecurity的数据如何获取10个嵌套的hashmap数据
Map<String, Object> requestBody=new HashMap<String, Object>();
requestBody.put("UPSSecurity", new HashMap<String, Object>());
requestBody.get("UPSSecurity").put("username","rohan");