Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two Hash Maps in Java

Hi I am working with HashMap in java and i have a scenario where i have to 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 two hashmaps my resulting hashmap will contain the Key as a Value of First HashMap1 and Value as a Value of second HashMap2.

HashMap3:
Key: SAPF  Value: Data1
Key: SAPL  Value: Data2
like image 313
Utsav Avatar asked Feb 28 '26 10:02

Utsav


1 Answers

Just iterate on the keys of HashMap1, and for each key, check if it's present in HashMap2. If it's present, add the values to 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>();

for (final String key : hm1.keySet()) {
    if (hm2.containsKey(key)) {
        hm3.put(hm1.get(key), hm2.get(key));
    }
}
like image 86
Florent Bayle Avatar answered Mar 03 '26 00:03

Florent Bayle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!