I recently attended an interview and was asked the following question.
There are two objects with same hashcode. I am inserting these two objects inside a hashmap.
hMap.put(a,a);
hMap.put(b,b);
where a.hashCode()==b.hashCode()
Now tell me how many objects will be there inside the hashmap?
I answered there will be only one object,since the hashcodes are equal the two objects will be equal and hashmap will not allow duplicate keys. Please tell me whether my understanding is correct or not?
There can be two different elements with the same hashcode. So your answer is incorrect. The only thing guaranteed is that if two elements have different hashcodes then they are different. When two elements have the same hashcode then Java uses the equals to further differentation.
So the answer is one or two objects.