Set view of mapping of java HashMap Example

Set view of mapping of java HashMap Example


Posted in : Java Posted on : December 2, 2010 at 5:48 PM Comments : [ 0 ]

In this example, We will show you how to get the mapping of keys and values of the HashMap object in java.

Set view of mapping of java HashMap Example

In this example, We will show you how to get the mapping of  keys and values of the HashMap object in java. To get a mapping of keys and values of HashMap object we use the entrySet() method of HashMap Class. This method returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

Example:- EntrySetHashMap.java

package devmanuals.com;

import java.util.*;

public class EntrySetHashMap {
	public static void main(String args[]) {
		HashMap hm = new HashMap();
		hm.put(1, "Gyan");
		hm.put(2, "Ankit");
		hm.put(3, "Arun");
		hm.put(4, "Anand");
		hm.put(5, "Ram");

		Set> set = hm.entrySet();
		for (Map.Entry me : set) {
			System.out.print(me.getKey() + ": ");
			System.out.println(me.getValue());
		}

	}
}
Output:-
1 : Gyan
2 : Ankit
3 : Arun
4 : Anand
5 : Ram

Download This Code.
Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics