HashMap in Java: Enhancements and Features Beyond Java 8

As we know HashMap is a data structure implements the Map Interface and is primarily used to store key-value pairs in a hash table. The beauty of HashMap's lies in their ability to perform retrieval operations with constant-time complexity.

In HashMap, the underlying data structure is an array of buckets. The default initial capacity of this array is 16.and in each bucket a key-value pair is stored, and we can store key -value pairs based on the hash code.

hashcode():it converts the string into the unique Integer format.

hashing: it will reduce the hash code value to get the index which is calculated using the modulo operation to fit within the hash table size.

when we are inserting the key value pairs in the hash table, when two or more key-value pairs have same hashcode then there will be collision occurs. There are two ways to resolve the collision.

1.Chaining:

Chaining means when a collision occurs the key-value pairs will be stored as LinkedList in that index.

suppose if the at worst case all the hash values are same then we use simple uniform hashing here we can assume that probability of every index should be the no of elements divided by the length of the table. bases on the probability we can insert the values.

2.Open addressing:

in open-addressing we can resize the table to store key-value pairs when ever collision occurs it uses probe technique.

In java 8, when ever Collison occurs we use LinkedList for chaining it make complexity more to reduce the time complexity instead of LinkedList using tree data structure where we can get the data with Time Complexity-O(logn).