HashMap in java

HashMap is a widely used data structure in the Java programming language. It is a part of the Java Collection Framework and is an implementation of the Map interface. It provides a fast and efficient way to store and retrieve data in the form of key-value pairs. In this blog post, we will discuss what HashMap is, how it works, and how to use it in Java, including examples of the remove(), clear() and size() methods.

What is HashMap in Java?

A HashMap is a collection that stores key-value pairs, where each key is unique and is associated with a specific value. It is implemented using a hash table, which is a data structure that allows for fast and efficient data retrieval.

A hash table uses a hash function to map keys to indexes in an array. The hash function takes the key as input and returns an index in the array, where the value associated with that key is stored. When we want to retrieve a value from the HashMap, we simply provide the key and the hash function maps it to the correct index in the array, where the value is stored.

Why use HashMap in Java?

HashMap is a highly efficient data structure that is widely used in Java because of its fast and efficient data retrieval capabilities. It is particularly useful in situations where we need to store a large amount of data and need to access it quickly.

HashMap is also useful in situations where we need to store data that is frequently accessed, such as a database or a cache. It is also commonly used in situations where we need to store and retrieve data based on a key, such as a lookup table or a dictionary.

How to use HashMap in Java

Using a HashMap in Java is relatively simple. To create a HashMap, we first need to import the java.util package, which contains the HashMap class.

The commonly available methods in HashMap are:

void put(Object key, Object value)This method helps to add key and values in the map
Object get(Object key)This method helps to retrieve the value associated with the given key from the map
int size()This method returns the size of the map
void clear()This method clears all the data from map and makes the map object as an empty

Add data into HashMap

Here is an example of creating a HashMap and adding data to it using put() method:

import java.util.HashMap;

HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);

Output of this example:

{one=1, two=2, three=3}

Once we have created the HashMap, we can add data to it using the put() method. The put() method takes two arguments: the key and the value. In the example above, we are adding three key-value pairs to the HashMap.

Get data from HashMap

We can also retrieve data from a HashMap using the get() method. For example, to retrieve the value associated with the key “two”, we can use the following code:

Integer value = map.get("two");

Output of this example:


In addition to the put() and get() methods, HashMap also provides other useful methods such as remove(), clear(), and size().

Remove data from HashMap

Here is an example of using the remove() method:


Output of this example:

{one=1, three=3}

Check the size of HashMap

To check the size of the HashMap we can use the size() method as shown in the example below:


Output of this example:


As you can see, the size() method returns the number of key-value pairs in the HashMap. In this example, it returns 2 because there are 2 key-value pairs in the map (one and three).

Clear data from HashMap

We may have a use case to make our HashMap object empty. In this case we can use the clear method clear() method as shown in the below example:


Output of this example: