If I remember correctly, every object that can be a key for a hash map should override `hashCode()`

method, so the general contract (from Javadoc) is

If two objects are equal according to the `equals(Object)`

method, then calling the `hashCode`

method on each of the two objects must produce the same integer result.

In other words:

`o1.equals(o2)`

then `o1.hashCode() == o2.hashCode()`

Hash maps internally use another `hash()`

function that create another hash code from `hashCode()`

values. This function at some point use modulus (for space efficiency), so different `hashCode()`

values can have equals `hash()`

value (keys are mapped with `hash()`

value).

This is not a problem, because if two keys in the map have equals `hash()`

value, will be compared with `equals()`

method when searched, to ensure they have the same key, and no two objects which, by coincidence, have the same hash code.

Some resource:

## Edit after the OP edit

I think `indexFor`

calculates the modulus. The function is

```
static int indexFor(int h, int length) {
return h & (length-1);
}
```

We know (from theory) that `a % b == a & (b - 1)`

iff *b* is 2^{n}. The length field (our "*b*") is multiple of 2^{n}:

Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions. This is critical because HashMap uses **power-of-two length** hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower bits. Note: Null keys always map to hash 0, thus index 0.

So different hash value can have same modulus, hence different object can have same index.