tutorialcup
HashMap Java
HashMap Java stores the data in the form of key-value pairs where the key data should be unique. We can access the values based on the corresponding key data. HashMap is present in Java's Collection framework and is part of java.util package. It works on the principle of the Hashing technique.
HashMap Java Hierarchy
The HashMap class in Java extends the Abstract class AbstractMap and implements the Map interface as shown below.
HashMap structure and working principle
HashMap in Java works on the principle of hashing technique. In hashing, we use hash functions to link key and value in a HashMap. The HashMap stores the key-value pairs in the form of an array of nodes where each entry is considered as a bucket. A bucket is nothing but an element in an array. Each node has 3 values: Key, value, and link to the next node. When more than 1 node shares the same index, it represents a linked list. Every node is mapped to an index in the bucket which is calculated using hashcode().
HashMap Performance
Performance of HashMap Java depends on the below parameters:
- Initial Capacity - It denotes how many buckets a HashMap can store when it is initialized. By default, it is 16 key-value pairs
- Load Factor - It is the percentage of capacity that needs to be increased. By default, it is 0.75
- Threshold - This is the product of load factor and capacity whose default value is 12 (16*0.75)
HashMap Java
HashMap Java stores the data in the form of key-value pairs where the key data should be unique. We can access the values based on the corresponding key data. HashMap is present in Java's Collection framework and is part of java.util package. It works on the principle of the Hashing technique.
HashMap Java Hierarchy
The HashMap class in Java extends the Abstract class AbstractMap and implements the Map interface as shown below.
HashMap structure and working principle
HashMap in Java works on the principle of hashing technique. In hashing, we use hash functions to link key and value in a HashMap. The HashMap stores the key-value pairs in the form of an array of nodes where each entry is considered as a bucket. A bucket is nothing but an element in an array. Each node has 3 values: Key, value, and link to the next node. When more than 1 node shares the same index, it represents a linked list. Every node is mapped to an index in the bucket which is calculated using hashcode().
HashMap Performance
Performance of HashMap Java depends on the below parameters:
- Initial Capacity - It denotes how many buckets a HashMap can store when it is initialized. By default, it is 16 key-value pairs
- Load Factor - It is the percentage of capacity that needs to be increased. By default, it is 0.75
- Threshold - This is the product of load factor and capacity whose default value is 12 (16*0.75)