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?

1
  • 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. Commented Aug 9, 2018 at 13:36

1 Answer 1

0
int[] arr = new int[10];
int i = Math.abs(hash % arr.size); // this always in this array
Sign up to request clarification or add additional context in comments.

1 Comment

to be entirely correct HashMap does not uses %... hashCodes can be negative and negative module is a negative result - go find a negative bucket

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.