There are several topics on this in stackoverflow as well as in web, I could not understand them clearly. I found this answer in stackoverflow by Nicklas and his representation which gave me some meaningful insight over the topic.
Some ASCII art
key.hashCode()
|
| 32-bit value
| hash table
V +------------+ +----------------------+
HashMap.hash() ----+ | reference | -> | key1 | value1 | null |
| |------------| +----------------------+
| | null |
| offset |------------| +---------------------+
+--------> | reference | -> | key1 | value1 | ref |
|------------| +---------------------+
| .... | |
+----------------+
V
+----------------------+
| key2 | value2 | null |
+----------------------+
What hashing function does Java use to implement Hashtable class?
Map aMap = new HashMap();
aMap.put(key,value);
- Can anyone explain me `what is an offset and its value?. Where is the offset value getting mapped?.
- what's that hash-table?. Is it an Array of Objects?, If so does each object have an key, value1 and a reference propery?.
Can anyone re-explain the above diagram step-by-step. I am not able to understand the Hashing mechanism behind HashMap.
HashMapisEntry<K,V>[] table."2. The 32-bit value is then transformed by a second hash function (...) into an offset inside the hash table"- is that unclear?put, thekeyvalue is hashed. The hash value is divided by the size of the hash table, and the remainder from that division (theoffsetin the above diagram) is used as the index to index into the actual hash table array. In the simplest case, where the hash table slot is empty, a new small object is created containing thekeyand thevalue. (refis null.) The reference (pointer) to that object is placed in the selected hash table slot.