entrySet() method of Map Interface in Java.

entrySet() method of Map Interface in Java.


Posted in : Core Java Posted on : February 18, 2011 at 7:49 PM Comments : [ 0 ]

This method gives back a view of set of the mappings contained into the underlying map. The set is enclosed with the map, which reflects the changes made in the

entrySet() method of Map Interface in Java.

In this section we will discuss how can entrySet() method be implemented in Map interface in java.

Syntax

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

This method returns a Set of the mappings into the underlying map.

This method gives back a view of set of the mappings contained into the underlying map. The set is enclosed with the map, which reflects the changes made in the set or  vice-versa.

Parameter description 

This method has no parameter but, it returns a Set view of the mappings contained into the underlying map.

Example of entrySet() method

In this example we will show you how does entrySet() method work in Map interface. This example will help you to understand how can you find the Set of mappings from the underlying map.

Example :

package devmanuals.com;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
public class MapEntrySet {
  public static void main (String args[]){
     Map mp = new HashMap();
          mp.put(1"A");
          mp.put(2"B");
          mp.put(3"C");
          System.out.println("Mapping = "+ mp);
          //Set view of the mappings contained in this map
          Set set = mp.entrySet();
          System.out.println("set =" + set);
          Iterator it =mp.entrySet().iterator();
         System.out.println("Set of mappings are :");
    while (it.hasNext()){
            Map.Entry me = (Map.Entryit.next();
             System.out.println(me);
    }
  }
}

Output :

All of the mappings {1=A, 2=B, 3=C}

set =[1=A, 2=B, 3=C]

Set of mappings are :

1=A

2=B

3=C

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics