As per the
SCJP book:
The answer is:
1) As the code stands the output will be 3
2)Add a note here If the hashCode() method is uncommented the output will be 2
I have tried this .. and its right..
But did not quite understand why it is so..
i know duplicate keys not allowed.. but need to understand what role does hashCode() play while 'inserting' in the map?
And also the code works without the hashCode() function... then what hachCode criteria (if it DOES contribute in decision of inserting an element or not )was used in this case?
Thanks in advance...
Ok.. rephrasing my question...
Without the hashCode() function, the code inserted
THREE elements in the map.. Even though the Keys are same(equals on the keys t1 and t2 returns true)...
and when you uncomment the hashCode(), the code inserts only
TWO elements... Please explain why?
Continue.. i printed the map (m) running the code with the bad hashCode() function on.. .. so the map now contains
{ToDos@9=cleanAttic, ToDos@9=payBills}
As per my understanding it should have been 2 elements 1) cleanAttic 2) doLaundry... why did it insert payBills instead of doLaundry(assuming it HAD to insert only One of the two as both had same keys)?