posted 15 years ago
Are you sure what you are saying and the program you have typed is complete and contains no error.
I run your program, it has this problem
return ((ToDos)0).day == this.day;
what is this zero 0, it should be o.
and second you have commented hashcode() function in ToDos?
Can I ask why have you commented, if you run this code without hashcode() it will add t2, as your program is adding.
But if you run this code with given hashcode it will not add t2.
Why?
Answer is How hashmap works ?
1. If you add any object in hashmap, hashmap will check its hashcode, for t2 hashcode in your program is 9. That is of t1 also.
2. Now if two objects have same hashcode than hashmap will call equals() method, t1.equals(t2) that is "Monday"=="Monday" that will return true. . If equals() return true, it will see t2 as duplicate of t1, so t2 will not be added, as HashMap does not contain duplicate keys.