I went through source code of HashMap and have a few questions. The PUT method takes the Key and Value and does
- the hashing function of the hashcode of the key.
calculate bucket location for this pair using the hash obtained from the previous step
public V put(K key, V value) { int hash = hash(key.hashCode()); int i = indexFor(hash, table.length); ..... } static int hash(int h) { h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); } static int indexFor(int h, int length) { return h & (length-1); }
Example:
- Creating a HashMap with size 10.
- call put(k,v) three times and assume these 3 occupies bucket loc 7 ,8 and 9
- call put 4th K,V pair and following happens
- hash() is called with key.hashcode() and hash calculated
- indexFor is calculated based on hash
Question:
- What if the calculated bucket location for the 4th k,v is out of the existing bounds? say location 11 ?
Thanks in advance Akh