Java HashMap associative multi dimensional array can not create or add elements -
okay have spent several hours trying wrap head around concept of hashmap in java not able figure out. have looked @ many tutorials none seem address exact requirement , cannot work.
i trying create associative multi dimensional array in java (or similar) can both save , retrieve array keys strings.
this how in php , explains best trying do:
//loop 1 - assign names myarray['en']['name'] = "english name"; myarray['fr']['name'] = "french name"; myarray['es']['name'] = "spanish name"; //loop 2 - assign description myarray['en']['desc'] = "english description"; myarray['fr']['desc'] = "french description"; myarray['es']['desc'] = "spanish description"; //loop 3 - assign keywords myarray['en']['keys'] = "english keywords"; myarray['fr']['keys'] = "french keywords"; myarray['es']['keys'] = "spanish keywords"; //later on in code able retrive value similar english_name = myarray['en']['name']; french_name = myarray['fr']['name']; spanish_name = myarray['es']['name'];
this tried in java not working:
hashmap<string, hashmap<string, string>> myarray = new hashmap<string, hashmap<string, string>>(); myarray.put("en" , put("name", "english name")); //gives me "cannot find symbol" @ second put myarray.put("en" , ("name", "english name")); //gives me "')' expected" after second comma
so sure simple missing please point out because frustrating!
thanks
edit:
so here working code on how implemented answer accepted:
import java.util.*; hashmap<string, hashmap<string, string>> finalarray = new hashmap<string, hashmap<string, string>>(); string[] langarray = {"en","fr","de","no","es"}; //initialize each language key ahead of time for(string lang : langarray) { // foreach lang in langarray if (!finalarray.containskey(lang)) { finalarray.put(lang, new hashmap<string, string>()); } } //loop 1 - assign names for(string lang : langarray) { string thename = lang + " name"; //go name somewhere finalarray.get(lang).put("name", thename); } //loop 2 - assign description for(string lang : langarray) { string thedesc = lang + " description"; //go description somewhere finalarray.get(lang).put("desc", thedesc); } //loop 3 - assign keywords for(string lang : langarray) { string thekeys = lang + " keywords"; //go keywords somewhere finalarray.get(lang).put("keys", thekeys); } //display output for(string lang : langarray) { system.out.println("language: " + lang); system.out.println(finalarray.get(lang).get("name")); system.out.println(finalarray.get(lang).get("desc")); system.out.println(finalarray.get(lang).get("keys")); } //example retrieve/get values string english_name = finalarray.get("en").get("name"); string french_desc = finalarray.get("fr").get("desc");
hashmap<string, hashmap<string, string>> myarray = new hashmap<string, hashmap<string, string>>(); if (!myarray.containskey("en")) { myarray.put("en", new hashmap<string, string>()); } myarray.get("en").put("name", "english name");
in java have explicit when creating object. in case first check if there hashmap
object stored in our outer hashmap
under key "en". if not, create empty one.
now put new value have first outer hashmap
, put new value.
Comments
Post a Comment