In the following code , size of the hashmap should come out as 2 as equals and hashcode methods are correctly overriden. it should view as t1 and t2 objects as duplicates.
import java.util.*;
public class MapEQ {
public static void main(
String s[]){
Map<ToDos,String> m=new HashMap<ToDos,String>();
ToDos t1=new ToDos("Monday");
ToDos t2=new ToDos("Monday");
ToDos t3=new ToDos("Tuesday");
m.put(t1, "Ravi");
m.put(t2, "Kumar");
m.put(t3, "Bansal");
System.out.println(m.size());
}
}
class ToDos{
String day;
ToDos(String d){
day=d;
}
public boolean equals(Object o){
return ((ToDos)o).day==this.day;
}
public int hashcode(){
return 9;
}
}
Output of this is coming as 3. Can anyone tell me why the t1 and t2 are not viewed as duplicates???