This write-up provides a thorough overview of HashMap in java and in this regard it covers the following concepts:
- What is a HashMap
- How to Work with HashMap
- How to Add and Remove Items
- How to Access Item of HashMap
- How to Find the Size of HashMap
- How to Iterate Through a HashMap
So, let’s start!
What is a HashMap
HashMap is a java class which exists in java.util package and can be used to store the items in the form of pairs, a pair that can have a key and a value. In HashMap the key must be the unique.
How to Work with HashMap
We have to follow some very simple but very crucial steps in order to work with HashMap:
In the very first step, we have to import the HashMap class from java.util package as follows:
Next, we have to create the object of the HashMap class and for this purpose, we have to follow the following syntax:
The object of the HashMap class will be created on the basis of the data types you want to work with. For example, if we have to store string type keys and integer type values then we can create the object of HashMap as follows:
In the same way, we can create the object of other data types as well and to do so, we have to provide the equivalent wrapper class of all these data types e.g. Integer for int, Character for char and so on.
How to Add Items in HashMap
The HashMap class has numerous methods that can be utilized to perform various functionalities. For example, the put() method can be used to add new items in a HashMap.
Example
In this example, initially, we import the HashMap class, and then, we create the object of the HashMap class. Lastly, we utilized the class’ object with put() method of the HashMap class to add new items:
public class HashMapExamples {
public static void main(String[] args) {
HashMap frameworks = new HashMap();
frameworks.put("C#", ".Net");
frameworks.put("Php", "Laravel");
frameworks.put("Python", "Django");
frameworks.put("Java", "Apache Wicket");
System.out.println(frameworks);
}
}
The above snippet creates an object of HashMap class and adds the keys and their respective values:
The output authenticates that put() method succeeds to add the keys and values in the HashMap.
How to Access HashMap Items
The get() method can be utilized to access the items of the list and to do so, all we need to do is put the key inside the parenthesis of the get() method.
Example
The below-given piece of code shows the appropriate way of utilizing the get() method:
In the above code snippet, we use the get() method and provides it the reference key. The output for the get() method will look like this:
The output shows that the get() method provides the relevant value for the specified key.
How to Remove Individual Item from HashMap
The remove() method can be used to delete some value from the HashMap and the syntax of the remove() method will be the same as the get() method.
Example
Let’s assume we have to delete the value of the “Django”, to do so, we will pass the reference key of the item i.e. Python in the parenthesis of the remove() method:
The code and its respective output are shown in the below-given screenshot:
We provide “Python” to the remove() method and it deletes the Python as well as its respective value. It confirms that the remove() method is working appropriately.
How to Remove All Items of HashMap
The HashMap class provides a clear() method that can be used to remove/clear all the items of HashMap. As it deletes all the entries from the HashMap so, there is no need to specify any Key in the parentheses.
The working of the clear() method and its output is provided in the below-given screenshot:
The empty HashMap verifies the working of the clear() method.
How to Find the Size of HashMap
HashMap class provides another handy method named size() that can be used to find the size of the HashMap.
Example
The snippet provided below shows the appropriate way of using the size() method.
The complete code and its respective output are is shown in the below-given screenshot:
From the output, it is clear that the size() method provides the accurate size of the HashMap.
How to Iterate Through HashMap
We can iterate through a HashMap using an iterator, for-each loop, and for-each method(). We can iterate through the items and values of the HashMap using the keySet() and values() method respectively.
Example
We utilize the for-each loop to iterate through the elements of HashMap:
The complete code and its output is shown in the below-given snippet:
The output verifies that the keySet() method iterates through the keys only.
We have to use the values() method in order to iterate through the values of the HashMap:
The output verifies the working of values() method as this time we get values instead of keys.
Conclusion
In order to utilize the functionalities of HashMap firstly we have to import the HashMap class into our project and then we have to create the object of that class. The HashMap class offers numerous methods to perform different functionalities e.g. put() and remove(), methods are used to insert and delete the entries of HashMap. This write-up detailed understanding of the HashMap class and its methods and for clarity of concepts multiple it considered multiple examples and provides relevant screenshots of output.