Hashmap:
1. it will work on key value.
2. It is implementend class of map interface
3. The default capacity of hm: 16
4. The load factor of hm: 0.75
5. It will store the element as a array of nodes
What is signature hashmap class:
HashMap<key,value> hm=new Hashmap<key,value>();
key+value= 1 entry
sets of entry added into hashmap: entrySet
entrySet(); it returns Entry interface
Entry: it is the subinterface of map interface. it has two methods 1) getKey(), 2)
getValue()
.........................................................
Internal working of hashmap?
What is hashcode?
>> It is the short integer value of object.
What is hashcode()?
>> It is used to get the hashcode of any object. It is the method of object class.
What is hashing or hashing function?
>> It is the process of calculating the hashcode of any object .
index collisions: when the two elements got the same index in bucket.
how can we get the value of hashmap if the two elements have the same index.
>> Where the equals method come into role, equals method will check the key with
stored key whoch is already in bucket, if it returns true then it will give us the
value, otherwise it will move to the next node. this process is continously run
till the value got.
..........................................................
What is the underlying data structure of hashmap?
>> Hashtable.
Calculating the index in hashmap:
index=hashcode of key% default capacity
Note: The hashcode of integer value will be same as the value.
Internal working of hashmap?
> Hashmap works on key and value pair it internally uses the hashtable data
structure where the bucket created and value store in that bucket. So how key and
value stored in hashtable is that first we have to calculate the hashcode of key
whether the key is integer or string value and we know that hashcode of integer
value is same as value. so for calculating the hashcode of any object we use the
hashcode method that is also known as hashing function so when we store the key and
value in hashmap it will create internally node of array where the each node holds
the key,value,hashcode and next element address. so for shifting on index number in
hashmap we use the formula is like (key%size of map) where the size of map is the
default capacity of the hashmap that is 16. so let's take an example if we store
the key as 30 and value as "xyz" for shifting on index number we have to use that
formula so
key% size of map= 30%16=14 it shifts on 14th index number in bucket of hashtable.
that is the whole process of shifting the key and value pair on index number in
bucket of hashtable.
Thanks.