Putting a HashMap inside a HashMap then populating the two maps.
Solution 1
Map.put
returns a string. gens is mapping String
to HashMap
. You are calling gens.put(string, string)
and should instead be calling gens.put(string, HashMap<string,string>)
Your code should read:
HashMap<String,String> acctyp = new HashMap<String,String>();
HashMap<String, HashMap<String,String>> gens = new HashMap<String,HashMap<String,String>>();
acctyp.put("'Open'","ACCTYP 01");
gens.put("'GEN01'", acctyp);
Solution 2
Because Map#put() returns a String value. When you do this : acctyp.put("'Open'","ACCTYP 01")
, it actually returns a String
hence gens.put("'GEN01'", acctyp.put("'Open'","ACCTYP 01"));
is a compilation error.
What I suppose you want is :
HashMap<String,String> acctyp = new HashMap<String,String>();
HashMap<String, HashMap<String,String>> gens = new
HashMap<String,HashMap<String,String>>();
acctyp.put("'Open'","ACCTYP 01");
acctyp.put("'Sheltered'","ACCTYP 02");
acctyp.put("'Spousal sheltered'","ACCTYP 03");
gens.put("'GEN01'", acctype);
Solution 3
Your code should like this
HashMap<String, HashMap<String,String>> gens = new HashMap<String,HashMap<String,String>>();
HashMap<String,String> acctyp = new HashMap<String,String>();
acctyp.put("'Open'","ACCTYP 01");
gens.put("'GEN01'", acctyp);
acctyp = new HashMap<String,String>();
acctyp.put("'Sheltered'","ACCTYP 02");
gens.put("'GEN02'", acctyp);
acctyp = new HashMap<String,String>();
acctyp.put("'Spousal sheltered'","ACCTYP 03");
gens.put("'GEN03'",acctyp );
acctyp = new HashMap<String,String>();
acctyp.put("'Education'","ACCTYP 06");
gens.put("'GEN06'",acctyp );
acctyp = new HashMap<String,String>();
acctyp.put("'Non-ded. sheltered'","ACCTYP 12");
gens.put("'GEN06'",acctyp );
acctyp = new HashMap<String,String>();
acctyp.put("'Structured Product'","ACCTYP 30");
gens.put("'GEN06'",acctyp );
acctyp = new HashMap<String,String>();
acctyp.put("'Emergency Cash'","ACCTYP 31");
gens.put("'GEN06'",acctyp );
Solution 4
You can also write this code if you want to fill the second hashmap later on:
acctype.put("'Open'",new Hashmap<String,String>());
acctype.put("''Sheltered'",new Hashmap<String,String>());
... So on
actttype.get("'Open'").put("'Sheltered'","ACCTYP 02")
acttype.get("'Sheltered'").put("'Spousal sheltered'","ACCTYP 03")
and so on ...
hope that helps too
Chris Quibell
Updated on February 06, 2021Comments
-
Chris Quibell over 3 years
I have to write a script for sql inserts. I have all the information that I need in order to write the inserts but there are three different pieces of information that I'm using and not just two. I know that I have to put a Map inside a Map but I am getting a compile error when I try to put anything into the maps.
HashMap<String,String> acctyp = new HashMap<String,String>(); HashMap<String, HashMap<String,String>> gens = new HashMap<String,HashMap<String,String>>(); gens.put("'GEN01'", acctyp.put("'Open'","ACCTYP 01")); gens.put("'GEN02'", acctyp.put("'Sheltered'","ACCTYP 02")); gens.put("'GEN03'", acctyp.put("'Spousal sheltered'","ACCTYP 03")); gens.put("'GEN06'", acctyp.put("'Education'","ACCTYP 06")); gens.put("'GEN12'", acctyp.put("'Non-ded. sheltered'","ACCTYP 12")); gens.put("'GEN30'", acctyp.put("'Structured Product'","ACCTYP 30")); gens.put("'GEN31'", acctyp.put("'Emergency Cash'","ACCTYP 31")); gens.put("'GEN85'", acctyp.put("'Insurance Savings'","ACCTYP 85")); gens.put("'GEN86'", acctyp.put("'Stock Option'","ACCTYP 86")); gens.put("'GEN94'", acctyp.put("'Business Other'","ACCTYP 94")); gens.put("'GEN95'", acctyp.put("'Personal use'","ACCTYP 95")); gens.put("'GEN97'", acctyp.put("'Universal Life'","ACCTYP 97"));
If anyone has any insight of why I'm getting a compile error on the puts then it would be greatly appreciated.