Performance of HashMap

The performance of HashMap depends on 2 parameters which are named as follows:

  1. Initial Capacity
  2. Load Factor

1. Initial Capacity – It is the capacity of HashMap at the time of its creation (It is the number of buckets a HashMap can hold when the HashMap is instantiated). In java, it is 2^4=16 initially, meaning it can hold 16 key-value pairs.

2. Load Factor – It is the percent value of the capacity after which the capacity of Hashmap is to be increased (It is the percentage fill of buckets after which Rehashing takes place). In java, it is 0.75f by default, meaning the rehashing takes place after filling 75% of the capacity.

3. Threshold – It is the product of Load Factor and Initial Capacity. In java, by default, it is (16 * 0.75 = 12). That is, Rehashing takes place after inserting 12 key-value pairs into the HashMap.

4. Rehashing – It is the process of doubling the capacity of the HashMap after it reaches its Threshold. In java, HashMap continues to rehash(by default) in the following sequence – 2^4, 2^5, 2^6, 2^7, …. so on. 

If the initial capacity is kept higher then rehashing will never be done. But by keeping it higher increases the time complexity of iteration. So it should be chosen very cleverly to increase performance. The expected number of values should be taken into account to set the initial capacity. The most generally preferred load factor value is 0.75 which provides a good deal between time and space costs. The load factor’s value varies between 0 and 1. 

Note: From Java 8 onward, Java has started using Self Balancing BST instead of a linked list for chaining. The advantage of self-balancing bst is, we get the worst case (when every key maps to the same slot) search time is O(Log n). 

HashMap in Java

In Java, HashMap is a part of Java’s collection since Java 1.2. This class is found in java.util package. It provides the basic implementation of the Map interface of Java. HashMap in Java stores the data in (Key, Value) pairs, and you can access them by an index of another type (e.g. an Integer). One object is used as a key (index) to another object (value). If you try to insert the duplicate key in HashMap, it will replace the element of the corresponding key. 

Similar Reads

What is HashMap?

Java HashMap is similar to HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map. To use this class and its methods, you need to import java.util.HashMap package or its superclass....

Java HashMap Examples

Below is the implementation of an example of Java HashMap:...

HashMap Declaration

...

Hierarchy of Java HashMap

public class HashMap extends AbstractMap implements Map, Cloneable, Serializable...

Creating HashMap in Java

...

Java HashMap Constructors

Let us understand how we can create HashMap in Java with an example mentioned below:...

Performing Various Operations on HashMap

...

Complexity of HashMap in Java

HashMap provides 4 constructors and the access modifier of each is public which are listed as follows:...

Important Features of HashMap

...

Internal Structure of HashMap

...

Performance of HashMap

...

Synchronized HashMap

...

Methods in HashMapassociate

1. Adding Elements in HashMap in Java...

Advantages of Java HashMap

...

Disadvantages of Java HashMap

...

Also, Read

...

FAQs on Java HashMap

...