Suppose this is my hashmap:
HashMap<String,String> map = new HashMap<>();
map.put("animal", "Golden Retriever");
map.put("size", "big");
map.put("color","light golden");
By the help of this Stack Overflow answer I figured out how to get one specific matching element key-value pair by passing the key-value testdata and the map itself to the function.
Now, I'd like to extend the functionality so that the app takes more than one key-value pairs as parameter and still figure out if there's any matching element.
I suppose that the way I could handle this task is to first change the parameters to the function so that it takes two hashmaps instead. In here there would be eventually a loop which runs the testdata over the original hashmap and see if there is any matching element or not.
What is the best way to deal with this task?
You can pass two HashMaps and loop through one and check if there is a match in the other one.
Method takes in haystack as the one to check for any needles in it. Returns boolean accordingly
public boolean ifContains(HashMap<String, String> haystack, HashMap<String, String> needles) {
for (String key : haystack.keySet()) {
if (needles.containsKey(key) && needles.get(key).equals(haystack.get(key))) {
return true;
}
}
return false;
}