java - What's the best way to sum two Map<String,String>? -


i have following maps.

map<string,string> map1= new hashmap<string, string>(){{        put("no1","123"); put("no2","5434"); put("no5","234");}};  map<string,string> map1 = new hashmap<string, string>(){{        put("no1","523"); put("no2","234"); put("no3","234");}};  sum(map1, map2); 

i want join them one, summing similar keyed values together. what;s best way using java 7 or guava libraries ?

expected output

map<string, string> output = { { "no1" ,"646"}, { "no2", "5668"}, {"no5","234"}, {"no3","234" }  } 

private static map<string, string> sum(map<string, string> map1, map<string, string> map2) {         map<string, string> result = new hashmap<string, string>();         result.putall(map1);         (string key : map2.keyset()) {             string value = result.get(key);             if (value != null) {                 integer newvalue = integer.valueof(value) + integer.valueof(map2.get(key));                 result.put(key, newvalue.tostring());             } else {                 result.put(key, map2.get(key));             }         }         return result;     } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -