Are hashCodes unique for Strings?

hashcode, java

Solution

A HashMap does use `equals()` to compare keys. It only uses `hashCode()` to find the bucket where the key is located, and thus drastically reduce the number of keys to compare with `equals()`.

Obviously, `hashCode()` can't produce unique values, since int is limited to 2^32 distinct values, and there are an infinity of possible String values.

In conclusion, the result of `hashCode()` is not a good fit for a key of a `Map`.

Problem

Recently, I came across a piece of code, where `Map<Integer, String>` is used, where `Integer`(key) is `hashCode` of some string and `String` value corresponding to that. Is this right thing to do? Because now, `equals` will not be called for the `String` when calling `get`. (`get` is also done using `hashCode()` method on String object. Or, hashCode(s) are unique for unique Strings? I checked `equals` od `String` class. There is logic written for that. I am confused.

Original source