collections - How to merge a list of maps in java? -


i have 2 lists of maps.

list<map<string,string>> catdatatocache = new arraylist(); list<map<string,string>> catdatatocache2 = new arraylist(); 

each map looks this

map('referer'=>'abc.com','category'=>'def.com') 

i have merge these two. tried list.addall(); garbled collection.

i believe you're trying merge mappings 2 or more maps. if so, using map#putall() without involving lists unless maps stored in there seems rather unlikely.

map<string, string> map1 = new hashmap<string, string>(); map1.put("referrer", "abc.com");  map<string, string> map2 = new hashmap<string, string>(); map2.put("category", "def.com");  map<string, string> map3 = new hashmap<string, string>();  map3.putall(map1); map3.putall(map2);  (map.entry<string, string> mapping : map3.entryset()) {     system.out.println(mapping.getkey() + " = " + mapping.getvalue()); } 

output :

category = def.com referrer = abc.com 


if you're getting maps within list, iterate on , use putall() shown above each map. if there multiple such lists, repeat iterator loop each 1 of them.

list<map<string,string>> listofmaps = new arraylist<map<string,string>>();  // initialize map1, map2, map3 same above  listofmaps.add(map1); listofmaps.add(map2);  (iterator<map<string, string>> iterator =                         listofmaps.iterator(); iterator.hasnext();) {     map3.putall(iterator.next()); }  (map.entry<string, string> mapping : map3.entryset()) {     system.out.println(mapping.getkey() + " = " + mapping.getvalue()); } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -