How to iterate over a collection of key-value pairs (associative array) in Java

7 Answers

0 votes
// Iterate Over Key–Value Pairs (Most Common)

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> dict = new HashMap<>();
        dict.put("Alice", 10);
        dict.put("Bob", 17);
        dict.put("Marley", 23);
        dict.put("Charlie", 36);

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



/*
run:

Marley = 23
Bob = 17
Alice = 10
Charlie = 36

*/

 



answered Mar 22 by avibootz
0 votes
// Iterate Over Keys Only

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> dict = new HashMap<>();
        dict.put("Alice", 10);
        dict.put("Bob", 17);
        dict.put("Marley", 23);
        dict.put("Charlie", 36);

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



/*
run:

Key: Marley
Key: Bob
Key: Alice
Key: Charlie

*/

 



answered Mar 22 by avibootz
0 votes
// Iterate Over Values Only

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> dict = new HashMap<>();
        dict.put("Alice", 10);
        dict.put("Bob", 17);
        dict.put("Marley", 23);
        dict.put("Charlie", 36);

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



/*
run:

Value: 23
Value: 17
Value: 10
Value: 36

*/

 



answered Mar 22 by avibootz
0 votes
// Lambda Style

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> dict = new HashMap<>();
        dict.put("Alice", 10);
        dict.put("Bob", 17);
        dict.put("Marley", 23);
        dict.put("Charlie", 36);

       dict.forEach((key, value) -> {
            System.out.println(key + " = " + value);
        });
    }
}



/*
run:

Marley = 23
Bob = 17
Alice = 10
Charlie = 36

*/

 



answered Mar 22 by avibootz
0 votes
// Filtering While Iterating

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> dict = new HashMap<>();
        dict.put("Alice", 10);
        dict.put("Bob", 17);
        dict.put("Marley", 23);
        dict.put("Charlie", 36);

        dict.entrySet().stream()
            .filter(entry -> entry.getValue() > 21)
            .forEach(entry -> 
                System.out.println(entry.getKey() + ": " + entry.getValue())
            );
    }
}



/*
run:

Marley: 23
Charlie: 36

*/

 



answered Mar 22 by avibootz
0 votes
// Sorted Iteration (Sort by Key)

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> dict = new HashMap<>();
        dict.put("Bob", 17);
        dict.put("Alice", 10);
        dict.put("Marley", 23);
        dict.put("Charlie", 36);

        dict.entrySet().stream()
            .sorted(Map.Entry.comparingByKey())
            .forEach(entry ->
                System.out.println(entry.getKey() + " = " + entry.getValue())
            );
    }
}



/*
run:

Alice = 10
Bob = 17
Charlie = 36
Marley = 23

*/

 



answered Mar 22 by avibootz
0 votes
// Sorted Iteration (Sort by Value)

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> dict = new HashMap<>();
        dict.put("Bob", 17);
        dict.put("Alice", 10);
        dict.put("Marley", 23);
        dict.put("Charlie", 36);

        dict.entrySet().stream()
            .sorted(Map.Entry.comparingByValue())
            .forEach(entry ->
                System.out.println(entry.getKey() + " = " + entry.getValue())
            );
    }
}



/*
run:

Alice = 10
Bob = 17
Marley = 23
Charlie = 36

*/

 



answered Mar 22 by avibootz

Related questions

...