如何在java中制作嵌套的HashMap

Roh*_*eja 3 java collections hashmap

我正在尝试将匿名哈希映射放入另一个哈希映射中:-

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

输出是:-

{UPSSecurity=null}
Run Code Online (Sandbox Code Playgroud)

小智 5

请使用这种方式来定义您的嵌套哈希图。

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);
Run Code Online (Sandbox Code Playgroud)

输出:

{UPSSecurity={username=rohan}}