What is HashMap in Java — Explained with examples.

Ateev Duggal
3 min readJul 5, 2022

--

HashMap in Java is a part of Java Collections Frameworks since Java 1.2 and provides the basic implementation of the Map interface in Java. It stores data in the form of Key and Value pairs where the key is a unique identifier used to associate each value on the Map. It means that every key is mapped to exactly one value and that we can use it to retrieve its corresponding value from the map.

One might ask, we already have so many data structures like Arrays, Linked List, etc, to store data, then what is the need to introduce a new one? The answer is simple, though the hashmap is used for the same thing as other data structures, it takes less time than them.

HashMap takes a constant time to do every operation like add, delete, replace, etc whereas other data structures take the time equal to the amount of data stored in it — O(n) and O(log n) if the data is sorted. But everything comes at a price. The price we pay for using the hashmap is that it increases the amount of auxiliary space which will be equal to the number of items stored.

Now that we have a basic understanding of what a hashmap is and why was it introduced, let’s see how HashMap is different from others.

Let’s begin…

Index

  1. What is HashMap in Java?
  2. Initialization of HashMap in Java
  3. Hierarchy of HashMap
  4. Internal Structure of HashMap
  5. Basic Operations of HashMap — Adding Elements, Changing Elements, Removing Elements, Iterating over HashMap
  6. Different methods of HashMap
  7. Factors affecting the performance of HashMap — Initial Capacity, Load Factor, Threshold
  8. Types of Constructors in HashMap
  9. Sorting in HashMap
  10. Summarizing
  11. Conclusion

What is HashMap in Java?

As told above, it is a type of Data Structure that is used to store data in the form of key and value pairs where both key and value are initialized using object/wrapper classes of the primitive data types like Integer, Boolean, String — which is already a wrapper class.

While the values can also be initialized as other data structures like ArrayList, Linked List, etc, the keys can’t.

One important thing to note is that the key should always be unique thus we know but why? The reason is that they act as the index to store the respective values in their respective places, which is not fixed as HashMaps are unsynchronized.

This means that there can be multiple keys for the same value but vice versa is not possible. If something did happen of this sort, the initial value of the respective key will be updated by the new value we have introduced. We will understand this in more detail in the Basic Operations section.

Initialization of HashMap in Java

To use a HashMap in our Java Program, we first have to import it from java.util package, and then use the below syntax to initialize it in the program

HashMap<K, V> numbers = new HashMap<K, V>();

In the above code, we have created a hashmap named numbers. Here, K represents the data type that will be used for keys while V represents the data type or structure for values.

Let’s understand thus with the help of an example –

HashMap<String, Integer> numbers = new HashMap<String Integer>();

In the above code, the String wrapper class has been used to initialize the keys while the Integer wrapper class has been used for values.

Continue Reading.

--

--

Ateev Duggal
Ateev Duggal

Written by Ateev Duggal

I am a front-end developer from India with over one year of experience in freelancing with skills like Git, HTML5, CSS3, Bootstrap 4&5, and React.

No responses yet