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?
The purpose of the hashCode() method is to provide a numeric representation of an object's contents so as to provide an alternate mechanism to loosely identify it. By default the hashCode() returns an integer that represents the internal memory address of the object.
If multiple objects return the same value from hashCode(), it means that they would be stored in the same bucket. If many objects are stored in the same bucket it means that on average it requires more comparison operations to look up a given object.
HashCode collisions Whenever two different objects have the same hash code, we call this a collision. A collision is nothing critical, it just means that there is more than one object in a single bucket, so a HashMap lookup has to look again to find the right object.
Two same strings/value must have the same hashcode, but the converse is not true. There might be another string which can match the same hash-code, so we can't derive the key using hash-code. The reason for two different string to have the same hash-code is due to the collision.
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.
There will either be one or two objects in the hashmap.
If the two objects are not equal, ie !a.equals(b)
, both will be stored.
No, the hashCode is an initial lookup for efficiency, but unless a.equals(b)==true
there will be two entries
There will be two objects in the hashmap, because they are not equals()
.
Here's the proof:
public static void main(String[] args) {
Object a = new Object() {
public int hashCode() {
return 1;
}
};
Object b = new Object() {
public int hashCode() {
return 1;
}
};
Map<Object, Object> map = new HashMap<Object, Object>();
map.put(a, a);
map.put(b, b);
System.out.println(map.size());
}
Output:
2
If I add an equals() method like this:
public boolean equals(Object obj) {
return obj.hashCode() == hashCode();
}
Output:
1
According to the javadoc for Object.equals():
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
This does not however mean that two objects that aren't equals()
can't share the same hashcode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With