The entrySet() method of java.util.HashMap class 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.
Signature
public Set<Map.Entry<K,V>> entrySet()
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
The following example shows the usage of java.util.HashMap.entrySet() method.
import java.util.HashMap; import java.util.Map; import java.util.Set; public class EntrySetMethodTreeSetExample { public static void main(String args[]) { // create an empty HashMap HashMap<Integer,String> hashMap1 = new HashMap<Integer,String>(); // use put() method to put elements to the HashMap hashMap1.put(1,"Element1"); hashMap1.put(2,"Element2"); hashMap1.put(3,"Element3"); hashMap1.put(4,"Element4"); hashMap1.put(5,"Element5"); //get the entrySet of hashMap1 Set<Map.Entry<Integer,String>> set1 = hashMap1.entrySet(); //Print the elements of hashMap1 System.out.println("**Elements of hashMap1**"); for (Map.Entry<Integer,String> me : set1) { System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } } }
Output
**Elements of hashMap1** 1: Element1 2: Element2 3: Element3 4: Element4 5: Element5
Leave a Reply