Here is a small sample program to demonstrate how to match keys or key and values of 2 different maps:
public class HashMapMatch {
public boolean hasMatchingKey(String key, Map m1, Map m2){
return m1.containsKey(key) && m2.containsKey(key);
}
public boolean hasMatchingKeyAndValue(String key, Map m1, Map m2){
if(hasMatchingKey(key, m1, m2)){
return m1.get(key).equals(m2.get(key));
}
return false;
}
public static void main(String[] args) {
HashMapMatch matcher = new HashMapMatch();
Map<String, Integer> map1 = new HashMap<>();
map1.put("One",1);
map1.put("Two",22);
map1.put("Three",3);
Map<String, Integer> map2 = new HashMap<>();
map2.put("One",1);
map2.put("Two",2);
map2.put("Four",4);
System.out.println("Has matching key? :" + matcher.hasMatchingKey("One", map1, map2));
System.out.println("Has matching key? :" + matcher.hasMatchingKey("Two", map1, map2));
System.out.println("Has matching key? :" + matcher.hasMatchingKey("Three", map1, map2));
System.out.println("Has matching key value: :" + matcher.hasMatchingKeyAndValue("One", map1,map2));
System.out.println("Has matching key value: :" + matcher.hasMatchingKeyAndValue("Two", map1,map2));
}
}
Produces the output:
Has matching key? :true
Has matching key? :true
Has matching key? :false
Has matching key value: :true
Has matching key value: :false