Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make nested HashMap in java

i'm trying to put an anonymous hashmap into another hashmap:-

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

Output is:-

{UPSSecurity=null}
like image 399
Rohan Dodeja Avatar asked Jun 01 '26 04:06

Rohan Dodeja


1 Answers

Please use this way to define your Nested Hashmap.

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);

Output:

{UPSSecurity={username=rohan}}

like image 134
Alok Ray Avatar answered Jun 03 '26 18:06

Alok Ray