Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

Java - iterate over HashMap

0 contributions
13 points

Short solution - using forEach from Java 8

Map<String, Integer> map = new HashMap<>();
map.forEach((key, value) -> {
    // use key and value variables
    System.out.println("Key: " + key + ", Value : " + value);
});

Short solution - using for loop and entrySet()

Map<String, Integer> map = new HashMap<>();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println("Key: " + entry.getKey() + ", Value : " + entry.getValue());
}

Short solution - using Iterator

Map<String, Integer> map = new HashMap<>();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
    Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) iter.next();
    System.out.println("Key: " + entry.getKey() + ", Value : " + entry.getValue());
}

Short solution - iterate over keys

Map<String, Integer> map = new HashMap<>();
map.keySet().forEach(key -> {
    // use key variable
    System.out.println("Key: " + key);
});

Short solution - iterate over values

Map<String, Integer> map = new HashMap<>();
map.values().forEach(value -> {
    // use key variable
    System.out.println("Value: " + value);
});

1. Using forEach from Java 8

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

public class Example1 {

    public static void main(String[] args) {

        Map<String, Integer> map = new HashMap<>();
        map.put("A", 10);
        map.put("B", 20);
        map.put("C", 30);

        map.forEach((key, value) -> {
            // use key and value variables
            System.out.println("Key: " + key + ", Value : " + value);
        });
    }
}

Output:

Key: A, Value : 10
Key: B, Value : 20
Key: C, Value : 30

2. Using for loop and entrySet()

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

public class Example2 {

    public static void main(String[] args) {

        Map<String, Integer> map = new HashMap<>();
        map.put("A", 10);
        map.put("B", 20);
        map.put("C", 30);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {

            String key = entry.getKey();
            Integer value = entry.getValue();

            System.out.println("Key: " + key + ", Value : " + value);
        }
    }
}

Output:

Key: A, Value : 10
Key: B, Value : 20
Key: C, Value : 30

3. Using Iterator

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

public class Example3 {

    public static void main(String[] args) {

        Map<String, Integer> map = new HashMap<>();
        map.put("A", 10);
        map.put("B", 20);
        map.put("C", 30);

        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, Integer> entry = 
                    (Map.Entry<String, Integer>) iter.next();

            String key = entry.getKey();
            Integer value = entry.getValue();

            System.out.println("Key: " + key + ", Value : " + value);
        }
    }
}

Output:

Key: A, Value : 10
Key: B, Value : 20
Key: C, Value : 30

4. Iterate over keys

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

public class Example4 {

    public static void main(String[] args) {

        Map<String, Integer> map = new HashMap<>();
        map.put("A", 10);
        map.put("B", 20);
        map.put("C", 30);

        map.keySet().forEach(key -> {
            // use key variable
            System.out.println("Key: " + key);
        });
    }
}

Output:

Key: A
Key: B
Key: C

5. Iterate over values

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

public class Example5 {

    public static void main(String[] args) {

        Map<String, Integer> map = new HashMap<>();
        map.put("A", 10);
        map.put("B", 20);
        map.put("C", 30);

        map.values().forEach(value -> {
            // use key variable
            System.out.println("Value: " + value);
        });
    }
}

Output:

Value: 10
Value: 20
Value: 30

Merged questions

  1. How do I iterate through HashMap in java?
  2. Best way to loop via map in java
  3. Java - iterate through any map
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more