×
☰ See All Chapters

Iterating Map in Java

  • Map doesn’t implement the Iterable interface. This means that we cannot cycle through a map using a for-each style for loop and we can’t obtain an iterator to a map. However we can obtain a collection-view of a map, which does allow the use of either the for loop or an iterator. 

  • The Map.Entry interface enables you to work with a map entry. The entrySet( ) method declared by the Map interface returns a Set containing the map entries. Each of these set elements is a Map.Entry object.  

  • Map.Entry is generic and is declared like this: 

interface Map.Entry<K, V>       

Here, K specifies the type of keys, and V specifies the type of values.

Methods present in Map interface which are used to obtain Collection view of a Map

Method

Description

Set<Map.Entry<K, V>> entrySet( )

 

Returns a Set that contains the entries in the map. The set contains objects of type Map.Entry. Thus, this method provides a set-view of the invoking map.       

Set<K> keySet( )

 

Returns a Set that contains the keys in the invoking map. This method provides a set-view of the keys in the invoking map.

Collection<V> values( )

 

Returns a collection containing the values in the map. This method provides a collection-view of the values in the map.

 

Methods present in Map.Entry interface which are used to iterate  Map

Method

Description

K getKey( )

Returns the key for this map entry.

V

getValue( ) Returns the value for this map entry.

Example to iterate Map

MapIterationDemo.java

import java.util.Collection;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

 

public class MapIterationDemo {

 

        public static void main(String[] args) {

                TreeMap map = new TreeMap();

                map.put(new Integer(103), "CCC");

                map.put(new Integer(105), "EEE");

                map.put(new Integer(102), "BBB");

                map.put(new Integer(101), "AAA");

                map.put(new Integer(104), "DDD");

 

                // Iterate using key

                Set s = map.keySet();

                Iterator itr = s.iterator();

                while (itr.hasNext()) {

                        Integer k = (Integer) itr.next();

                        System.out.println(k);

                        // using key can get value

                        String v = (String) map.get(k);

                        System.out.print(k + "\t" + v + "\n");

                }

 

                // Iterate using values

                Collection c = map.values();

                Iterator itr1 = c.iterator();

                while (itr1.hasNext()) {

                        String name = (String) itr1.next();

                        System.out.println(name);

                }

 

                // iterate using both key and value

                Set s1 = map.entrySet();

                Iterator itr2 = s1.iterator();

                while (itr2.hasNext()) {

                        Map.Entry me = (Map.Entry) itr2.next();

                        Integer k = (Integer) me.getKey();

                        String v = me.getValue().toString();

                        System.out.print(k + "\t" + v + "\n");

                }

        }

}

Output:

101

101        AAA

102

102        BBB

103

103        CCC

104

104        DDD

105

105        EEE

AAA

BBB

CCC

DDD

EEE

101        AAA

102        BBB

103        CCC

104        DDD

105        EEE

 


All Chapters
Author