java - Map inside map iteration -
i have following nested map defined in class:
private map<string, map<string, string>> messagesbyfacttypeandcategory; public void setmessagesbyfacttypeandcategory(map<string, map<string, string>> messagesbyfacttypeandcategory) { this.messagesbyfacttypeandcategory = messagesbyfacttypeandcategory; } public map<string, map<string, string>> getmessagesbyfacttypeandcategory() { if (messagesbyfacttypeandcategory == null) { return maps.newhashmap(); } return messagesbyfacttypeandcategory; } i'm trying unable traverse messagesbyfacttypeandcategory map , data inside display in console.
below code tried far:
map<string, map<string, string>> newmap = executionresult.getmessagesbyfacttypeandcategory(); set set = newmap.entryset(); iterator iterator = set.iterator(); while(iterator.hasnext()) { map.entry mentry = (map.entry)iterator.next(); system.out.print("key is: "+ mentry.getkey() + " & value is: "); system.out.println(mentry.getvalue()); } any appreciated!
if need traverse entries of both maps, can follows:
for (entry<string, map<string, string>> outermapentry : messagesbyfacttypeandcategory.entryset()) { // outermapentry system.out.println(outermapentry.getkey() + " => " + outermapentry.getvalue()); (entry<string, string> innermapentry : outermapentry.getvalue().entryset()) { // inner map entry system.out.println(innermapentry.getkey() + " => " + innermapentry.getvalue()); } } edit. explanation. each outer map entry pair of string key , map<string, string> value. can both key , value on each iteration. instance, can print key , value are. or can traverse value (it map) , print each value entry separately in inner loop.
i believe it's quite clear how iterate "simple" map map<string, string>(see how efficiently iterate on each entry in map?), there no difficulty traversing inner map.
Comments
Post a Comment