How put/Get method works in HasmMap;
First we will see put method and then get method.
For examples- map.put("key","value");
1-First the check table size
( if (table == EMPTY_TABLE) {
inflateTable(threshold);
})
So why this EMPTY_TABLE and inflateTable method?
Previous version of Java ,they are providing a default size of HashMap whenever HashMap object is created. But after some analysis they realize that its waste of memory in case of when hashmap is created (map=new HashMap)but programmer is not using it .So they made change that the size of hashMap will be assign only when HashMap will be called put method only.
2- Then they are checking for null references of given key,because HashMap allow only one null key and multiple null values.In case of this checking ,if client is providing null more than one time then they will override previous null key but they will not throw any exception or error.
Interview-Many time interviewer asked ,what will be happen when any body is providiing null key more than one time and we say that it will throw exception.But right answer is No Exception or Error only it will override previous null key
putForNullKey(value);
3- Then they will do most important things of HashMap.We called it Hashing.
So question is How they designed this hashing things for storing key/value pair.First they call hash(key) method to get a hashvalue which is integer(int).And in internals they are using two way of method.One when key is String and threasold!=0
final int hash(Object k) {
int h = hashSeed;
if (0 != h && k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode();
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
Another one is when they call ,if key is not a String and h==0 and it will return hashcode value.
In the hashing if key is String ,they are calling hash value of each character of String.
In stringHash32 they are using algo based on 32 bit and 64 bits/128 bits.
4- int i = indexFor(hash, table.length);
i is value of exact bucket index where they will store key.
5- After finding a bucket index,they will check that -is any key already store that position.
Here the importance of equals method come.
Overriding Value Part Of Entry,when Key is Equals.
Suppose there is already a key stored on that position.they will iterate over there .They will find Entry obejct by calling
table[i].Because hashMap used LinkedList to store key/value pair.
Each entry object contain a Next pointer which will pointing next element of it.So it will iterate .and lets suppose that they find equals value like below.
i is bucket position.
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
-If key and hashvalue is equals,then they will override value part of entry object.
-If HashValue is same and key are diffrent ,its possible because two different key can produce a similar hashkey.In this case they will
call next step addEntry.
6- In above 5th step we don't need any new buckedt for storig Entry.So in this step.
Exceeds the Threshold
We will check bucket size ,If bucket size is greater than threshold(capacity*loadfactor).In resizing process,they are making size
of HashMap double and again they will call hashing techniques to copy element.
After creation and load of a old value to new hashtable ,they will try to put the new key/value in table.
Not Exceeds The Threshold-
They will go to exact bucket index and copy previous store Entry if available. If there is no previous store Entry .Then
they will invoke constructor new Entry().
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}
In construction party we will store new Entry object at first place and next part will address the previous store entry.So always newly created Entry will be at first place.
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
Now Look into get method of hashMap
How get Method workss in HahsMap. map.get("key"); In this method,first we check null references ,and return if any value associated with Null value. Then we are finding hashvalue for key and then find index,after find index we will find exact bucket number and fetch associated Entry object and iterate over there .Becuase it can point next element or Entry. If Key references are equals OR key content are equals .then it will return associated object which is value part of Entry. So after look up the code of HashMap we have reached on a conclusion Two similar key can't hold two places. Questions- Whenever we are calling get and put method of HashMap,Is ther no role of Value part of HashMap except being store into HashMap. Are we not using value part inside for equals and hashcode. Answer-?
How get Method workss in HahsMap. map.get("key"); In this method,first we check null references ,and return if any value associated with Null value. Then we are finding hashvalue for key and then find index,after find index we will find exact bucket number and fetch associated Entry object and iterate over there .Becuase it can point next element or Entry. If Key references are equals OR key content are equals .then it will return associated object which is value part of Entry. So after look up the code of HashMap we have reached on a conclusion Two similar key can't hold two places. Questions- Whenever we are calling get and put method of HashMap,Is ther no role of Value part of HashMap except being store into HashMap. Are we not using value part inside for equals and hashcode. Answer-?