问题
If two objects of the same class have the same hashCode in Java then how would they be stored in a HashMap
/ HashTable
? What is the actual architecture for hashcode and memory address. Where does hashCode reside in memory?
Example: There is a class A
. when creating objects a1
and a2
then they will represent some memory address but I overrode hashcode every time same. When I read an article then I found that hashcode functions generate a hashcode from the memory address. this means the memory address will same if hashcode is same. Please clear my doubt.
public class A {
@Override
public int hashCode() {
return 1;
}
public static void main(String args[]) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.hashCode());
System.out.println(a2.hashCode());
}
}
回答1:
No two objects (that exist at the same time) can have the same memory address.
They can have the same hash code, though hashCode
implementations try to avoid that. And the default implementation of hashCode
doesn't have to be based on the object's memory address (though it can be).
So if two objects have the same hash code, you can't assume that they have the same memory address. In fact, if two variables refer to different objects (i.e. comparing them with ==
returns false
), they definitely do not have the same address.
The article you read about hash codes being based on memory addresses was referring to the default implementation of the hashCode
method in the Object
class. If you override hashCode
in a subclass, you're not using that default implementation anymore. Your return 1
has nothing to do with memory addresses.
回答2:
The default object version of hashCode()
is based on a memory address. When you override the hashCode()
method and return a different value it does not change the Object's memory address. Nor does returning a constant 1 break HashMap
, but it does severely effect performance.
回答3:
System.out.println(a1==a2); The Result is false.
回答4:
Note that since objects commonly define their own implementation of hashcode and equals, based on their contents/value rather than object identity, hashcode is NOT reliably related to the object's address.
The identity hashcode -- which is also the default hashcode implementation provided by java.lang.Object -- may or may not be related to the object address, depending on how this JRE's garbage collector manages memory.
回答5:
Hashcode and memory addresses are two different things. Hashcode is used to identify the bucket position in the memory to store the key. But two non equal objects with the same hashcode will reside in the same bucket but at a different memory address.
how would they be stored in a HashMap / HashTable?Hashcode does not reside in the memory anywhere.
Any hashed collection uses hashed buckets architecture to decide where to store the object. This helps in the quick retrieval of objects. This is the saving mechanism:
Objects with different hashcode and non equal(
equals()
return false on two object) : Will be saved in different hashed bucketsObjects with different hashcode and equal: Will be saved at the same hashed bucket but in a linked list
Object with same hashcode and equal: Will overwrite each other when saved
What is the actual architecture for hashcode and memory address.
Where does hashCode reside in memory?
It is always calculated when you try to put/retrieve an element in a hashed collection. And hashcode method provides the logic.
回答6:
I think root of the question is to understand the relation between hash value and memory location. Hash map/table use array for storing key and values. The value obtained from hash(key) function is used to determine the index in the array. If you go one more step deep in native side, memory address will be (memory address of first element of array + index). At this memory location, address of actual object will be stored.
As others have already answered, if two objects have same hash value then those objects will be in same bucket. Meaning, at the same index value of the array. But, in this case to avoid collision each element of array could be linked list. Thus, objects with same hash value will be added to a linked list.
回答7:
If two objects have the same hashcode and are of the same class - the second will replace the first if both are added to a Hashtable/HashMap.
来源:https://stackoverflow.com/questions/25539351/if-two-objects-have-same-hashcode-then-what-about-their-memory-addresses