How to store the elements of Map in a Set ?

The Map.Entry enables you to work with a map entry.
The entrySet() method of the Map returns a set which elements are the Map.Entry Object.
Example:
import java.util.*;
class demo1
{
public static void main(String[] args){
HashMap hm=new HashMap();
hm.put(4,"bhabani");
hm.put(1,"samar");
hm.put(8,"gaura");
hm.put(3,"milan");
Set> set=hm.entrySet();
for(Map.Entry me:set){
System.out.print(me.getKey()+":");
System.out.println(me.getValue()+":");
//System.out.println(me);//Alternative way to display
}}};

No comments:

Post a Comment