Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

hashmap
java
loop
communitycreator

How to loop through HashMap in Java

Programming Bytes

We can loop through a HashMap in a number of different ways. In this shot, we will learn three common ways to loop a HashMap.

1. forEach method

The forEach method loops through all the entries of the HashMap. The forEach method takes the BiConsumerRepresents a function that accepts two input arguments and returns no result function interface as input and processes the entries of the Map.

import java.util.HashMap;
import java.util.function.BiConsumer;

class LoopHashMapByForEach {
    public static void main(String[] args) {

        // create an HashMap
        HashMap<Integer, String> numbers = new HashMap<>();

        numbers.put(1, "One");
        numbers.put(2, "Two");
        numbers.put(3, "Three");
        
        numbers.forEach( (key, value) -> {
            System.out.println(key +" - " +value);
        });

        System.out.println("---------");
        System.out.println("Using Bi Consumer");

        BiConsumer<Integer, String> biConsumer = (key, value) -> System.out.println(key + " # " + value);
        numbers.forEach(biConsumer);

    }
}

In the code above, we have created a HashMap and used the forEach method to loop the HashMap. We passed a lambdaLambda expressions are an anonymous function (function without name). We can pass the Lambda expression as a parameter to another function. We can pass the lambda expression as an argument for the method which is expecting a functional interface as an argument. function as an argument to the forEach method. Then, we created a BiConsumerRepresents a function that accepts two input arguments and returns no result function and passed it to the forEach method.

2. keySet of HashMap

The keySet method returns all the keys of the HashMap as a Set.

import java.util.HashMap;
import java.util.Set;

class LoopHashMapByForEach {
    public static void main(String[] args) {

        // create an HashMap
        HashMap<Integer, String> numbers = new HashMap<>();

        numbers.put(1, "One");
        numbers.put(2, "Two");
        numbers.put(3, "Three");

        Set<Integer> keys = numbers.keySet();

        for (Integer key: keys){
            System.out.println(key + " # " + numbers.get(key));
        }

    }
}

In the code above, we used the keySet method of the HashMap to get all the keys of the HashMap, and looped all the keys to print values of all of them.

3. entrySet() method

The entrySet method returns a Set with key-value mappings as an Entry.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

class LoopHashMapByForEach {
    public static void main(String[] args) {

        // create an HashMap
        HashMap<Integer, String> numbers = new HashMap<>();

        numbers.put(1, "One");
        numbers.put(2, "Two");
        numbers.put(3, "Three");

        Set<Map.Entry<Integer, String>> entries = numbers.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + " # " + value);
        }
    }
}

In the code above, we used the entrySet() method to get a Set which contains all the mapping of the HashMap. Then, we looped the entrySet and printed the key and value.

RELATED TAGS

hashmap
java
loop
communitycreator
RELATED COURSES

View all Courses

Keep Exploring