Description

On this document we will be showing a java example on how to use the entrySet() method of HashMap Class. Basically this method is to return a set which is a view of the HashMap object. Because this method is backed with Map class, any change on our HashMap will get instantly reflected on the Set and vice versa.

In dealing with HashMap methods, as a general rule you must always watch out for Generics such that if we have declared our map like HashMap<Integer,String> map = new HashMap<Integer,String>();  then the returned keys are expected to be Integer object type or its subclass and the values as String.

Important notes for entrySet() method:

  • specified by keySet in interface Map<K,V>
  • overrides keySet in class AbstractMap<K,V>

Method Syntax

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

Method Argument

Data Type Parameter Description
N/A N/A N/A

Method Returns

The entrySet() method returns a set view of the mappings contained in this map.

Compatibility

Requires Java 1.2 and up

Java HashMap entrySet() Example

Below is a java code demonstrates the use of entrySet() method of HashMap class. The example presented might be simple however it shows the behavior of the keySet() method.

package com.javatutorialhq.java.examples;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
 * This example source code demonstrates the use of  
 * entrySet() method of HashMap class
 */

public class HashMapEntrySetExample {

	public static void main(String[] args){

		// initialize the HashMap object with Integer as key and String as value
		HashMap<Integer, String> mapEmployee = new HashMap<Integer, String>();
		
		// populate the student map
		mapEmployee.put(1287, "Aquilino Pimentel");
		mapEmployee.put(3125, "Travis Davis");
		mapEmployee.put(9972, "Marianne Laste");
		
		//get the keys
		Set<Map.Entry<Integer, String>> mappingSet = mapEmployee.entrySet();
		System.out.println("Initial value of keys:"+mappingSet);
		
		// add another key value mapping
		mapEmployee.put(4581, "Kate Visor");
		
		// print the values which the set contains
		System.out.println("New set of keys:"+mappingSet);
	}	
}

Sample Output

Below is the sample output when you run the above example.

HashHamp entrySet() method example Output

There would be two output on the console when you try to run the provided example. First, the initial value of key-value mapping were printed and the next one are the new set of key-value mapping after we added additional key-value mapping on our HashMap. At first glance this is a straightforward example but did you notice that we have not invoked the entrySet after we have put additional key on our HashMap and yet the new key were added to the mappingSet. This is the inherent behavior of entrySet() method when you modify the HashMap object the Set would also be modified. The Set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.