Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Java - iterate over values in HashMap

0 contributions
1 points

Short solution - using forEach() from Java 8:

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

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

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

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

for (Integer value : map.values()) {
    System.out.println("Value: " + value);
}

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

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

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

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

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

Output:

Value: 10
Value: 20
Value: 30

2. Using for loop and Map values()

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 (Integer value : map.values()) {
            System.out.println("Value: " + value);
        }
    }
}

Output:

Value: 10
Value: 20
Value: 30

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

We can also iterate over entire map and use only values 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 value variable
            System.out.println("Value : " + value);
        });
    }
}

Output:

Value : 10
Value : 20
Value : 30

4. Using entrySet()

We can also iterate over entire map with entrySet() and use only values.

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("Value : " + value);
        }
    }
}

Output:

Value : 10
Value : 20
Value : 30

Merged questions

  1. How do I iterate through values only of HashMap in java?
  2. Best way to loop via values of map in java
  3. Java - iterate through values of 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