java - How to multiply two values with the same key name from different linkedHashMaps? -


i have 2 linkedhashmaps same length same key names values different. example :

map<string, double> map1= new linkedhashmap<string, double>();   map1.put("a", 2.3);     map1.put("b", 6.1); map1.put("c", 5.3);  map<string, double> map2= new linkedhashmap<string, double>();   map2.put("a", 10.3);     map2.put("b", 60.1); map2.put("c", 0.3); 

how multiply each value map1 same corresponding key in map2 (a 2.3*10.3, b 6.1*60.1 etc etc) , save results new linkedhashmap?

i tried no luck.

map<string, double> map3= new linkedhashmap<string, double>();                 double valuemap1 = map1.get(key);                 double valuemap2 = map2.get(key1);              for(string key : map1.keyset())                 for(string key1 : map2.keyset()){             if(map1.containskey(key1)){                   double newvalue =valuemap1* valuemap2;                   map3.put(key1, newvalue);         }       } 

here loop should solve problem

map<string, double> map3= new linkedhashmap<string, double>(); for(string key : map1.keyset()) {     if(map2.containskey(key)) {        map3.put(key, map1.get(key) * map2.get(key);     } } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -