hashmap - How to compare two Hash Maps in Java -
hashmap - How to compare two Hash Maps in Java -
hi working hashmap in java , have scenario have compare 2 hashmaps
hashmap1: key: bof value: sapf key: bom value: sapm key: bol value: sapl hashmap2: key: bof value: data1 key: bol value: data2
and after comparing these 2 hashmaps resulting hashmap contain key value of first hashmap1 , value value of sec hashmap2.
hashmap3: key: sapf value: data1 key: sapl value: data2
just iterate on keys of hashmap1
, , each key, check if it's nowadays in hashmap2
. if it's present, add together values hashmap3
:
final map<string, string> hm1 = new hashmap<string, string>(); hm1.put("bof", "sapf"); hm1.put("bom", "sapm"); hm1.put("bol", "sapl"); final map<string, string> hm2 = new hashmap<string, string>(); hm2.put("bof", "data1"); hm2.put("bol", "data2"); final map<string, string> hm3 = new hashmap<string, string>(); (final string key : hm1.keyset()) { if (hm2.containskey(key)) { hm3.put(hm1.get(key), hm2.get(key)); } }
java hashmap comparison
Comments
Post a Comment