Originally posted by Campbell Ritchie:
It is an exam question; they are expecting us to find mistakes.
I understand that, my point was the mistakes are so numerous and egriegous that I wouldn't have any idea where to start. Usually with a question worded in that manner they're getting at something in particular.
Originally posted by Campbell Ritchie:
What you are doing is overloading the put method, but they have swapped key and element round, so you are putting the E as the K and "this" as the E. The if(!containKey(element)) statement means it will not happen twice for the same "element."
There is no E in a Map, it's just a Key-Value pair and the Keys are a Set, they may not be duplicated. This is just an absurd implementation of Set where the Set is the Keys of a Map. They add "this" as the Value simply because they have to add
something. The fact that you have a bunch of irrelevent Values exposed should be the first hint at some of the undesired behavior of a Map.
Originally posted by Campbell Ritchie:
If you pass a key and an element as arguments, you will get the expected behaviour.
What? Huh? No. In this case the key IS the element and the desired behavior of a Set cannot be achieved by subclassing HashMap without inheriting a whole lot of undesired behavior too.