public class VO {
public int hashcode()
{
return 0;
}
public boolean equals(Object obj)
{
return true;
}
public static void main(String args[])
{
VO vo1 = new VO();
VO vo2 = new VO();
Map<VO,Integer> map = new HashMap<VO, Integer>();
map.put(vo1, 1);
map.put(vo2, 1);
System.out.println(map.size());
}
}
I am getting the output is :2
But as per my knowledge the output is 1.
When i am placing an element in map it will check the hashcode for the key,if that hashcode is same then it will go to check equals.If both the methods returns same it will override the previous value.
In my case both the methods are(hashcode and equals) returns 0 and true.So finally there must be one element in the map.But here i am getting size as 2.
What might be the reason.Thanks in dvance...