And the following statments are correct:
1. As the code stands the output will be 3
2. If the hashCode() method is uncommented the output will be 2.
I thought the output should be always 2 regardless the hashcode as the overidden equal method will be the one to determine if the keys are dupublicate or not. In this case, if day are the same then they should be count as the same key. I am confused now, Can anyone please help
A hashmap will hash to the bucket first -- and then compares equality with the items in the bucket only. This is why a hashmap has only an order of 1 for most operations -- does not depend on the number of items in the map. Meaning.... if hashed correctly, the should be only a few items in each bucket.
In the second case (uncommented), you are basically putting everything in the same bucket -- hence, the equals() method will be used to
test equality for everything. You are also no longer order of one for most operations.
In the first case, you are using the hashcode() method of the Object class, to hash your ToDos object. This method does a really good job at distributing objects, and your three ToDos objects are likely going to three different buckets. And your equals() method is not being used.
Henry