0

I created one hashmap so default size will be 16. I overide the equals and hashcode method. And I am returning 20 from hashcode method. So now we have bucket 0-15 in map but hashcode value is 20 so which bucket will be using for insert this key?

  • The `HashMap` does not use the value returned by the `hashCode` methods directly. It uses them for some kind of modulo operation, using its own hashing method which obeys some nice mathematical properties. – Zabuzard Aug 09 '18 at 13:36

1 Answers1

0
int[] arr = new int[10];
int i = Math.abs(hash % arr.size); // this always in this array
oleg.cherednik
  • 15,340
  • 4
  • 20
  • 31
  • 1
    to be entirely correct `HashMap` *does not* uses `%`... hashCodes can be negative and negative module is a negative result - go find a negative bucket – Eugene Aug 14 '18 at 11:57