How to Iterate HashMap in Java?

HashMap is a piece of Java’s assortment giving the essential execution of the Map point of interaction of Java by putting away the information in (Key, Value) sets to get to them by a file of another kind. One item is recorded as a key (file) to another article (esteem). On the off chance that you attempt to embed the copy key, it will supplant the component of the relating key. To utilize this class and its strategies, it is important to import java.util.HashMap bundle or its superclass.

There is a various number of ways of repeating over HashMap of which 5 are recorded as beneath:

  • Emphasize through a HashMap EntrySet utilizing Iterators.
  • Emphasize through HashMap KeySet utilizing Iterator.
  • Emphasize HashMap utilizing for-each circle.
  • Emphasizing through a HashMap utilizing Lambda Expressions.
  • Circle through a HashMap utilizing Stream API.

How to Iterate HashMap in Java?

1: Using a for circle to repeat through a HashMap.

Execution: In the code given underneath, entrySet() is utilized to return a set perspective on planned components. From the code given beneath:

set.getValue() to get esteem from the set.
set.getKey() to get key from the set.

Java

// Java Program to Iterate over HashMap
// Importing Map and HashMap classes
// from package names java.util
import java.util.HashMap;
import java.util.Map;
// Class for iterating HashMap using for loop
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a HashMap
        Map<String, String> foodTable
            = new HashMap<String, String>();
        // Inserting elements to the adobe HashMap
        // Elements- Key value pairs using put() method
        foodTable.put("A", "Angular");
        foodTable.put("J", "Java");
        foodTable.put("P", "Python");
        foodTable.put("H", "Hibernate");
        // Iterating HashMap through for loop
        for (Map.Entry<String, String> set :
             foodTable.entrySet()) {
            // Printing all elements of a Map
            System.out.println(set.getKey() + " = "
                               + set.getValue());
        }
    }
}
Output

P = Python
A = Angular
H = Hibernate
J = Java

2: Using a forEach to iterate through a HashMap.

Java

// Java Program to Iterate over HashMap
// Iterating HashMap using forEach
// Importing Map and HashMap classes
// from package names java.util
import java.util.HashMap;
import java.util.Map;
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating hash map
        Map<Character, String> charType
            = new HashMap<Character, String>();
        // Inserting data in the hash map.
        charType.put('J', "Java");
        charType.put('H', "Hibernate");
        charType.put('P', "Python");
        charType.put('A', "Angular");
        // Iterating HashMap through forEach and
        // Printing all. elements in a Map
        charType.forEach(
            (key, value)
                -> System.out.println(key + " = " + value));
    }
}
Output

P = Python
A = Angular
H = Hibernate
J = Java

3: Using an iterator to iterate through a HashMap.

Example:

Java

// Java Program to Iterate over HashMap
// Using Iterator
// Importing classes from java.util package
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class GFG {
    // Main driver method
    public static void main(String[] arguments)
    {
        // Creating Hash map
        Map<Integer, String> intType
            = new HashMap<Integer, String>();
        // Inserting data(Key-value pairs) in hashmap
        intType.put(1, "First");
        intType.put(2, "Second");
        intType.put(3, "Third");
        intType.put(4, "Fourth");
        // Iterator
        Iterator<Entry<Integer, String> > new_Iterator
            = intType.entrySet().iterator();
        // Iterating every set of entry in the HashMap
        while (new_Iterator.hasNext()) {
            Map.Entry<Integer, String> new_Map
                = (Map.Entry<Integer, String>)
                      new_Iterator.next();
            // Displaying HashMap
            System.out.println(new_Map.getKey() + " = "
                               + new_Map.getValue());
        }
    }
}
Output

1 = First
2 = Second
3 = Third
4 = Fourth

4: Iterating through a HashMap using Lambda Expressions

parameter -> expression

Example: 

Java

// Iterating HashMap using Lambda Expressions- forEach()
// Importing Map and HashMap classes
// from java.util package
import java.util.HashMap;
import java.util.Map;
// Class
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating hash map
        Map<Character, String> charType
            = new HashMap<Character, String>();
        // Inserting elements(key-value pairs)
        // in the hash map ( Custom inputs)
        charType.put('A', "Apple");
        charType.put('B', "Basketball");
        charType.put('C', "Cat");
        charType.put('D', "Dog");
        // Iterating through forEach and
        // printing the elements
        charType.forEach(
            (key, value)
                -> System.out.println(key + " = " + value));
    }
}
Output

A = Apple
B = Basketball
C = Cat
D = Dog

5: Loop through a HashMap using Stream API

Steps:

  • First invoke entrySet().stream() method which inturn returns Stream object.
  • Next forEach method, which iterates the input objects that are in the entrySet(). See the below code.

Example:

Java

// Java Program to Iterate over HashMap
// Loop through a HashMap using Stream API
// Importing classes from
// package named 'java.util'
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
// HashMap class
public class GFG {
    // Main driver method
    public static void main(String[] arguments)
    {
        // Creating hash map
        Map<Integer, String> intType
            = new HashMap<Integer, String>();
        // Inserting data(key-value pairs) in HashMap
        // Custom inputs
        intType.put(1, "First");
        intType.put(2, "Second");
        intType.put(3, "Third");
        intType.put(4, "Fourth");
        // Iterating every set of entry in the HashMap, and
        // printing all elements of it
        intType.entrySet().stream().forEach(
            input
            -> System.out.println(input.getKey() + " : "
                                  + input.getValue()));
    }
}
Output

1 : First
2 : Second
3 : Third
4 : Fourth

Also ReadHow to Install Minecraft on Linux?

Leave a Reply

Your email address will not be published. Required fields are marked *