What is the entrySet() method in Java?

The HashMap class provides the java.util.HashMap.entrySet() method in Java. It is used to create and then return a ‘set’ of the same elements that are already present in the HashMap. It can be used with a loop to iterate over all the entries of a HashMap.

EntrySet() Method Header

The header of the entrySet() method is given below. It returns the set view of all entries containing key-value pairs. To use it in our code we need to import java.util.HashMap package.

public Set<Map.Entry<key, value>> entrySet()

Parameters

The entrySet() method does not take any parameters.

EntrySet() Return Type

The java.util.HashMap.entrySet() method returns an instance of the class set.

Example


import java.util.HashMap;

public class Driver1 {

	public static void main(String[] args) {

		// declare a custom hash map
		HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

		// add data to the hash map
		hashMap.put(1, "Monday");
		hashMap.put(2, "Tuesday");
		hashMap.put(3, "Wednesday");
		hashMap.put(4, "Thursday");
		hashMap.put(5, "Friday");
		hashMap.put(6, "Saturday");
		hashMap.put(7, "Sunday");

		// print the original hash map
		System.out.println("Original HashMap: " + hashMap + '\n');
		// print the entrySet of the hash map
		System.out.println("HashMap.entrySet(): " + hashMap.entrySet() + '\n');

		// Try adding null value in the hash map
		hashMap.put(0, null);
		System.out.println("hashMap.put(0, null)");
		System.out.println("HashMap.entrySet(): " + hashMap.entrySet() + '\n');

		// Try adding null key and value pair to the hash map
		hashMap.put(null, null);
		System.out.println("hashMap.put(null, null)");
		System.out.println("HashMap.entrySet(): " + hashMap.entrySet() + '\n');

		// Try adding a null character as a value in the hash map 
		hashMap.put(null, "\0");
		System.out.println("hashMap.put(null, \"\\0\")");
		System.out.println("HashMap.entrySet(): " + hashMap.entrySet() + '\n');

	}
}

Output

Original HashMap: {1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday} HashMap.entrySet(): [1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday] hashMap.put(0, null) HashMap.entrySet(): [0=null, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday] hashMap.put(null, null) HashMap.entrySet(): [0=null, null=null, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday] hashMap.put(null, "\0") HashMap.entrySet(): [0=null, null= , 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday]

Explanation

In the code snippet above, we have first of all imported the java.util.HashMap package. It allows us to use the HashMap and entrySet() method. We then create a hashMap which is the object of the HashMap class. Our hashMap contains strings as values. The keys are integers. We then fill the hashMap. There are seven entries in total. We then use the setEntry() method to return a set view and then print in on the console.

Conclusion

This was a simple implementation of the Java HashMap entrySet() method. Hope you are familiar with the use of the method after going through this post. As always, you’re encouraged to practise over and over to get good at it. Till then, keep practicing and keep growing!