I have two HashMap. I need to join the two hashMap by their key.
Map<String, String> firstMap = new HashMap<String, String>();
Map<String, String> secondMap = new HashMap<String, String>();
firstMap= [{K1,V1},{K2,V2}]
secondMap= [{K2,V2},{K3,V3}]
I need my third map to have
thirdMap= [{K2,V2}]
Kindly help me out. Thanks
This code should do what you need:
Map<String, String> thirdMap = new HashMap<String, String>();
for (String key : firstMap.keySet()) {
if (secondMap.containsKey(key)) {
thirdMap.put(key, firstMap.get(key));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With