5 Best Ways to Iterate Over HashMap in Java

In this post, we will discuss five best ways to iterate over a HashMap in Java with examples.
  1. Iterate through a HashMap EntrySet using Iterator
  2. Iterate through HashMap KeySet using Iterator
  3. Iterate HashMap using For-each Loop
  4. Iterating through a HashMap using Lambda Expressions
  5. Loop through a HashMap using Stream API
Learn about lambda expressions at https://www.javaguides.net/2018/07/java-8-lambda-expressions.html

Video Tutorial

The examples are very well explained in the below video tutorial: 

1. Iterate through a HashMap EntrySet using Iterator

package com.java.tutorials.iterations;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 5 Best Ways to Iterate Over HashMap in Java
 * @author Ramesh Fadatare
 *
 */
public class IterateHashMapExample {
    public static void main(String[] args) {
        // 1. Iterate through a HashMap EntrySet using Iterator
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "C");
        coursesMap.put(2, "C++");
        coursesMap.put(3, "Java");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        Iterator < Entry < Integer, String >> iterator = coursesMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry < Integer, String > entry = iterator.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework

2. Iterate through HashMap KeySet using Iterator

package com.java.tutorials.iterations;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 5 Best Ways to Iterate Over HashMap in Java
 * @author Ramesh Fadatare
 *
 */
public class IterateHashMapExample {
    public static void main(String[] args) {
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "C");
        coursesMap.put(2, "C++");
        coursesMap.put(3, "Java");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        // 2. Iterate through HashMap KeySet using Iterator
        Iterator < Integer > iterator = coursesMap.keySet().iterator();
        while (iterator.hasNext()) {
            Integer key = iterator.next();
            System.out.println(key);
            System.out.println(coursesMap.get(key));
        }
    }
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework

3. Iterate HashMap using For-each Loop

package com.java.tutorials.iterations;

import java.util.HashMap;
import java.util.Map;

/**
 * 5 Best Ways to Iterate Over HashMap in Java
 * @author Ramesh Fadatare
 *
 */
public class IterateHashMapExample {
    public static void main(String[] args) {
        // 1. Iterate through a HashMap EntrySet using Iterator
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "C");
        coursesMap.put(2, "C++");
        coursesMap.put(3, "Java");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        // 3. Iterate HashMap using For-each Loop
        for (Map.Entry < Integer, String > entry: coursesMap.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework

4. Iterating through a HashMap using Lambda Expressions

package com.java.tutorials.iterations;

import java.util.HashMap;
import java.util.Map;

/**
 * 5 Best Ways to Iterate Over HashMap in Java
 * @author Ramesh Fadatare
 *
 */
public class IterateHashMapExample {
    public static void main(String[] args) {
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "C");
        coursesMap.put(2, "C++");
        coursesMap.put(3, "Java");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        // 4. Iterating through a HashMap using Lambda Expressions
        coursesMap.forEach((key, value) -> {
            System.out.println(key);
            System.out.println(value);
        });
    }
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework

5. Loop through a HashMap using Stream API

package com.java.tutorials.iterations;

import java.util.HashMap;
import java.util.Map;

/**
 * 5 Best Ways to Iterate Over HashMap in Java
 * @author Ramesh Fadatare
 *
 */
public class IterateHashMapExample {
    public static void main(String[] args) {
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "C");
        coursesMap.put(2, "C++");
        coursesMap.put(3, "Java");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");

        // 5. Loop through a HashMap using Stream API
        coursesMap.entrySet().stream().forEach((entry) - > {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
    }
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework

Related HashMap Tutorials

Comments

  1. Hello, I want to translate this article into Chinese. Can I get translation authorization?

    ReplyDelete

Post a Comment