Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hashCode implementation in Java String [duplicate]

Tags:

java

Just curious, in String's hashCode implementation what is the reason behind extra references creation in a hashCode implementation (v 1.8.0_65):

public int hashCode() {
    int h = hash;
    if (h == 0 && value.length > 0) {
        char val[] = value;

        for (int i = 0; i < value.length; i++) {
            h = 31 * h + val[i];
        }
        hash = h;
    }
    return h;
}

Taking into consideration that value is final and created in constructor only (i.e. threadsafe) why do we need variable val[] reference here?

I.e. will this work:

public int hashCode() {
    if (hash == 0 && value.length > 0) {
        int h = 0;
        for (int i = 0; i < value.length; i++) {
            h = 31 * h + value[i];
        }
        hash = h;
    }
    return hash;
}

?

In addition to copying values from heap to stack to speed things up it is also about race conditions described in comments by @zapl. Which was not obvious to me before his comment.

like image 585
Konstantin Kulagin Avatar asked Oct 30 '15 11:10

Konstantin Kulagin


People also ask

Can two strings have same Hashcode in Java?

Yes, it is possible for two Strings to have the same hashcode - If you take a look at the Wikipedia article, you will see that both "FB" and "Ea" have the same hashcode. There is nothing in the method contract saying a hashCode() should be used to compare for equality, you want to use equals() for that.

Can hashcode be duplicated?

Threads are different and hash codes are the same. it is possible that two instances can give the same hash code, but this is unlikely.

Is hashcode for string in Java unique?

They are not unique. Show activity on this post. By doing it's own hashing of the key String, that code risks the chance that two different key strings will generate the same integer map key and the code will fail in some situations. In general, the code should probably be using Map<String,String> .

What happens if hashcode for multiple keys is same?

When two unequal objects have the same hash value, this causes a collision in the hash table, because both objects want to be in the same slot (sometimes called a bucket).


1 Answers

It seems like the intent is to put hash and the value handle explicitly on the stack

like image 171
ControlAltDel Avatar answered Sep 19 '22 08:09

ControlAltDel