Traverse through HashMap in Java
In this tutorial, we will discuss traversing through HashMap in Java
Introduction:
HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K, V>, In which K, V is represented as the Key and Value and <,> represents the collection. HashMap class is available in Java. util package. It gives the fundamental execution of the Map interface in Java. It stores the information in (Key, Value) matches, and we can get to them by a record of another sort (for example, an Integer). One item is utilized as a key (index) to another entity (value). If we attempt to embed the copy key, it will replace the component of the comparing key.
HashMap is like HashTable; however, it is unsynchronized. It permits storing the invalid keys too, but there ought to be just a null key object, and there can be quite a few invalid qualities. This class makes no certifications concerning the request for the guide. To utilize HashMap and its techniques, we have to import Java. util.HashMap package or its superclass.
HashMap stores the data in (Key, Value) matches, and we can get to them by a file of another feature. HashMap class carries out a Map interface which permits us to store keys. It inside utilizes a quick hashing procedure.
Traversal Methods:
There are specific methods in traversal through HashMap, which can iterate over mapping in which the key-value pair is matched.
Methods:
- Using an Iterator
- Using for-each loop
- Using forEach() Method
Method 1: Using an Iterator
Iterator is a connection point that is an interface found in Java.util package and is utilized to emphasize through a collection. As such, there is nothing exceptional to talk about iterators, so; We will propose techniques for the Iterator point of interaction being utilized to cross over HashMap.
hm.entrySet() - is utilized to recover all the key-esteem matches called Map. Entries and stores inside into a set.
hm.entrySet().iterator() - returns an iterator that goes about as a cursor, takes a look at the important feature of the set, and continues until the end.
hmIterator.hasNext() - checks for the below features in the set and returns a boolean
hmIterator.next() - returns the below mentioned element (Map.Entry) from the set.
mapElement.getKey() - returns the key to the related Map.Entry
mapElement.getValue() - return the worth of the linked Map.Entry
Program:
// HashMap Program Using Iterator
import java.util.*;
class Jtp
{
// Main method
public static void main(String s[])
{
// Hashmap of string-integer pairs
HashMap<String, Integer> hm
= new HashMap<String, Integer>();
// It contains names and marks
// Adding mappings to HashMap using object name.put() method
hm.put("Ramesh", 72);
hm.put("Arun", 81);
hm.put("Suresh", 48);
// Printing all the elements inserted into a HashMap
System.out.println("HashMap is " + hm);
// Creating an iterator
Iterator hmIterator = hm.entrySet().iterator();
System.out.println("HashMap after adding 10 marks:");
// Iterating through Hashmap after adding ten marks
while (hmIterator.hasNext()) {
Map.Entry mapElement = (Map.Entry)hmIterator.next();
int marks = ((int)mapElement.getValue() + 10);
// Displaying marks after adding ten marks and corresponding names
System.out.println(mapElement.getKey() + " : " + marks);
}
}
}
Output:
HashMap is {Suresh=48, Arun=81, Ramesh=72}
HashMap after adding ten marks:
Suresh : 58
Arun : 91
Ramesh : 82
Method 2: Using for-each loop
In this method, we will use for-each loop function:
Program:
// HashMap program using for-each Loop
import java.util.*;
// Main class
class Jtp
{
// Main method
public static void main(String s[])
{
// HashMap of string-integer pairs
HashMap<String, Integer> hm
= new HashMap<String, Integer>();
// It contains the name and marks of the student
// Adding mappings to HashMap using object name.put() method
hm.put("Ramesh", 72);
hm.put("Arun", 81);
hm.put("Suresh", 48);
// Displaying elements of HashMap
System.out.println("Hashmap is" + hm);
// Display message to add ten marks
System.out.println(
"HashMap after adding 10 marks:");
// HashMap Using for-each loop
for (Map.Entry<String,Integer> mapElement : hm.entrySet())
{
String key = mapElement.getKey();
// Adding ten marks to all the students
int value = (mapElement.getValue() + 10);
// Displaying marks after adding ten marks and corresponding names
System.out.println(key + " : " + value);
}
}
}
Output:
Hashmap is{Suresh=48, Arun=81, Ramesh=72}
HashMap after adding ten marks:
Suresh : 58
Arun : 91
Ramesh : 82
Method 3: Using forEach() Method
In this method, we will use forEach() function:
Code:
// HashMap Program Using forEach() method
import java.util.*;
class Jtp
{
// Main method
public static void main(String s[])
{
// Hashmap of string-integer pairs
HashMap<String, Integer> hm
= new HashMap<String, Integer>();
// It contains names and marks
// Adding mappings to HashMap using object name.put() method
hm.put("Ramesh", 72);
hm.put("Arun", 81);
hm.put("Suresh", 48);
// Printing all the elements inserted into a HashMap
System.out.println("HashMap is " + hm);
// Display message to add ten marks
System.out.println("HashMap after adding 10 marks:");
// Looping of HashMap and adding ten marks
// using HashMap.forEach() method
hm.forEach((k, v)-> System.out.println(k + " : "+ (v + 10)));
}
}
Output:
HashMap is {Suresh=48, Arun=81, Ramesh=72}
HashMap after adding ten marks:
Suresh : 58
Arun : 91
Ramesh : 82