• Register

Share:- Whatsapp Facebook Facebook

------------------------------------- Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

How to iterate map in java?

0 votes
1,582 views
How to iterate map in java? Please give some example.
asked Sep 3, 2013 in Java by Harendra Kumar

2 Answers

0 votes
package com;

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

public class MapItr {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //Create map object
        Map<String, String> maps = new HashMap<String, String>();
        //Add element in map
        maps.put("1", "RK");
        maps.put("2", "KK");
        maps.put("3", "HF");
        maps.put("4", "NU");
        
        //Iterating map through keyset
        for(String key: maps.keySet()){
            System.out.println(key + " : " +maps.get(key));
        }
        
        //Iterating map through entryset
        for(Map.Entry<String, String> ent: maps.entrySet()){
            System.out.println(ent.getKey() + " : "+ent.getValue());
        }

        //Iterating map through iterator
        Iterator<Entry<String, String>> it = maps.entrySet().iterator();
        while(it.hasNext()){
            Entry<String, String> el = it.next();
            System.out.println(el.getKey() + " : " + el.getValue());
        }
    }

}
answered Sep 3, 2013 by Bhim Kumar
0 votes
There are following ways for iterating a Map in Java. As we know that all maps (HashMap, TreeMap, LinkedHashMap, Hashtable, etc.) in Java implement the Map interface. A map cannot contain duplicate keys; each key can map to at most one value. The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings.

Method 1: -  Iterating map using for loop by using entrySet() method:-
                System.out.println("Iterating using for loop by using entrySet() method");
        // If you want both the key and value use entrySet()
        for (Map.Entry<Integer, String> entry : myMap.entrySet()) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + " : " + value);
        }
Output:-
Iterating using for loop by using entrySet() method
1 : Vinod
2 : Abhishek
3 : Suman
4 : SOnu
5 : Maynak
6 : Sohan

Method 2: - Iterating over keys or values using a For-Each loop
                System.out.println("Iterating over keys or values using a For-Each loop");
        //Iterating over keys or values using a For-Each loop
        // Iterating over keys only
        for (Integer key : myMap.keySet()) {
            System.out.println("Key = " + key);
        }
        // Iterating over values only
        for (String value : myMap.values()) {
            System.out.println("Value = " + value);
        }
Output:-
Iterating over keys or values using a For-Each loop
Key = 1
Key = 2
Key = 3
Key = 4
Key = 5
Key = 6
Value = Vinod
Value = Abhishek
Value = Suman
Value = SOnu
Value = Maynak
Value = Sohan

Method 3: - Iterating map using Iterator
                System.out.println("Iterating map using Iterator");
        //Iterating using Iterator
        Iterator<Map.Entry<Integer, String>> entries = myMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<Integer, String> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
Output:-
Iterating map using Iterator
Key = 1, Value = Vinod
Key = 2, Value = Abhishek
Key = 3, Value = Suman
Key = 4, Value = SOnu
Key = 5, Value = Maynak
Key = 6, Value = Sohan

Method 4: - Iterating using for loop by using keySet() method
                System.out.println("Iterating using for loop by using keySet() method");
        //Iterate through the keySet() of the map
        for (Integer key : myMap.keySet()) {
            System.out.println(key + " : " + myMap.get(key));
        }
Output:-
Iterating using for loop by using keySet() method
1 : Vinod
2 : Abhishek
3 : Suman
4 : SOnu
5 : Maynak
6 : Sohan

Method 5: - Iterating using for loop by using values() method
                System.out.println("Iterating using for loop by using values() method");
        //If you only need the values we can use values()
        for (String value : myMap.values()) {
            System.out.println(value);
        }
Output:-
Iterating using for loop by using values() method
Vinod
Abhishek
Suman
SOnu
Maynak
Sohan

Method 6: - Iterating map in java 8 lambda using foreach
               System.out.println("Iterating using foreach");
        myMap.forEach((key, value)->{
            System.out.println(key + " : " + value);
        });
Output:-
Iterating using foreach
1 : Vinod
2 : Abhishek
3 : Suman
4 : SOnu
5 : Maynak
6 : Sohan

Method 7: - Iterating map in java 8 lambda in a single line using foreach
                System.out.println("Iterating using foreach");
        myMap.forEach((key, value)->System.out.println(key + " : " + value));
Output:-
Iterating using foreach
1 : Vinod
2 : Abhishek
3 : Suman
4 : SOnu
5 : Maynak
6 : Sohan

Method 8: - Iterating map in java 8 lambda entryset with foreach
                System.out.println("\nIterating using entryset with foreach");
        myMap.entrySet().forEach((entry)->{
            System.out.println(entry.getKey() + " : " + entry.getValue());
        });
Output:-
Iterating using entryset with foreach
1 : Vinod
2 : Abhishek
3 : Suman
4 : SOnu
5 : Maynak
6 : Sohan

Method 9: - Iterating map in java 8 lambda using entryset
                System.out.println("\nIterating using entryset");
        Set<Entry<Integer, String>> entryset = myMap.entrySet();
        for(Entry<Integer, String> ent: entryset) {
            System.out.println(ent.getKey() + " : " + ent.getValue());
        }
Output:-
Iterating using entryset
1 : Vinod
2 : Abhishek
3 : Suman
4 : SOnu
5 : Maynak
6 : Sohan
answered Dec 30, 2018 by Developerhelpway (1,400 points)
...