HashMap is basically an Array of HashMap.Entry objects (Entry is an inner class of HashMap).
What happens when a key/value is inserted in HashMap ?
- HashCode of the key is calculated, and that value is assigned to the hashCode variable of EntryClass.
- Then, using hashCode we get the index of the bucket where it will be stored.
- If the bucket is having a pre-existing element, the new element is inserted with the last element pointing to new one — essentially making the bucket a LinkedList.
Now, when you query it to get the value for a key, it comes in O(1). But most important thing is that it comes at the cost of more space(memory).