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());
}
}