I think I'm still missing one more part to this. The problem requires that the method test not only if every key in the first map is contained in the second map, but also that they map to the same value that's in the second map. So I need to come up with another piece of code for that.
Joined: Feb 08, 2008
Altered the code as follows, but this does not work to catch a value that is different from the first map in the second:
Just about yes. There are some improvements to be made though:
Now my code with some more explanations:
Now the containsKey is necessary to differentiate between not having the value (get returns null) or having the value null.
My code will perform just a bit better than yours because you iterate over the key set twice: once during containsAll and once in the for-loop. That leaves your two gets versus my get and containsKey, but in most implementations those are equally fast.
Rob, I tried my code with your suggested improvements. It compiled but did not return the correct results. I very much appreciate your comments, though. They helped me understand the code, especially "if a then b else c". I've been trying to understand that syntax and making it way too hard.
Our textbook mentions entrySet just to say that there is such a thing but they're not going to cover it. So we can't use it in our solutions. I did try your code though and got the following compile error. Note that I changed the map names map1 and map2 to aMap and bMap respectively for reasons unrelated to this method: