Languages
[Edit]
EN

Java - iterate over keys in HashMap

8 points
Created by:
Emelia
517

Short solution - using forEach() from Java 8:

Map<String, Integer> map = new HashMap<>();

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

Short solution - using for loop and Map keySet():

Map<String, Integer> map = new HashMap<>();

for (String key : map.keySet()) {
    System.out.println("Key: " + key);
}

Short solution - iterate over entire map and use only keys with forEach() from Java 8:

Map<String, Integer> map = new HashMap<>();

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

Short solution - iterate over entire map with entrySet() and use only keys:

Map<String, Integer> map = new HashMap<>();

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    String key = entry.getKey();
    // Integer value = entry.getValue();

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

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.keySet().forEach(key -> {
            // use key variable
            System.out.println("Key: " + key);
        });
    }
}

Output:

Key: A
Key: B
Key: C

2. Using for loop and Map keySet()

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 (String key : map.keySet()) {
            System.out.println("Key: " + key);
        }
    }
}

Output:

Key: A
Key: B
Key: C

3. Using forEach() from Java 8 on entire map

We can also iterate over entire map and use only keys with forEach() from Java 8

import java.util.HashMap;
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);

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

Output:

Key : A
Key : B
Key : C

4. Using entrySet()

We can also iterate over entire map with entrySet() and use only 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);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            // Integer value = entry.getValue();

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

Output:

Key : A
Key : B
Key : C

Merged questions

  1. How do I iterate through keys only of HashMap in java?
  2. Best way to loop via keys of map in java
  3. Java - iterate through keys of any map
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java Collections - HashMap

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join