简体   繁体   中英

if two objects have same hashcode then what about their memory addresses

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

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 (ie 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.

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.

System.out.println(a1==a2); The Result is false.

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.

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 buckets

  • Objects 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.

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.

I wanna make some supplementary instructions for the answer having most votes @Wyzard

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.

In fact, the default implementation of the hashCode is not only one, instead, there are almost 6 ways (according to OpenJDK). You can get this conclusion by refer to the method get_next_hash found in the .../share/vm/prims/jvm.h and .../share/vm/prims/jvm.cpp , which is critical to calculate the hash code . For your convenience, I've posted a screenshot below: enter image description of the method here

What's more, different version of JDK just uses different way to calculate the hash code. For example, JDK1.8 product the result utilizing thread state combined with xorshift, seeing from enter image description here .

Now turn to the algorithm of generating a hash code base on the object memory address. Provided that it just simply cast the memory address to a hash code, what will happen if GC occurs during the Java running time? The address of the object may change, and then the original hash code will change too. Hence there are must more other extra operation to make sure the hash code will remain consistent using this kind of algorithm to implement the hashCode() .

如果两个对象具有相同的哈希码并且属于同一类-如果将两个对象都添加到Hashtable / HashMap中,则第二个对象将替换第一个对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM