How to use entrySet() method in Java

Published in the Java Developer group

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.

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.

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. How to use entrySet() method in Java - 1

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!
Comments (5)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Celina Agon Mir Level 0, United States
27 June 2022
I go by the Name Celina Agon Mir from Paris, currently residing here in CA Los Angeles, it's been 9 years have been working here in United State as a Nurse, I always like using Instagram to while away time for myself and I came in contact with a Chinese profile picture, the guy was really awesome and he told me he was into broker trading that I could invest little with $5,000 and within a year I will have enough to buy myself a home and a hospital, so I decided to invest but after 8 months I tried to withdraw and I was asked for more and all this while he encouraged me to keep investing and I had already invested 183k USD with him on his broker platform and we started dating so that really makes me confident in him but it was too late to know I have been scammed after he successfully rips me and put me in a debt of $459k. The night I was to take poison and killed myself I came across ( ghostchampionwizard@gmail.com ) and they called me by WhatsApp after which I contacted them firstly, they talked to me and I was calm, less than 72 hours I have heavily restored back better than my normal life... all thanks to almighty great ghostchampionwizard Email: GHOSTCHAMPIONWIZARD (at) Gmail dot com Website: https://championhacker0.wixsite.com/my-site/about
Celina Agon Mir Level 0, United States
27 June 2022
I go by the Name Celina Agon Mir from Paris, currently residing here in CA Los Angeles, it's been 9 years have been working here in United State as a Nurse, I always like using Instagram to while away time for myself and I came in contact with a Chinese profile picture, the guy was really awesome and he told me he was into broker trading that I could invest little with $5,000 and within a year I will have enough to buy myself a home and a hospital, so I decided to invest but after 8 months I tried to withdraw and I was asked for more and all this while he encouraged me to keep investing and I had already invested 183k USD with him on his broker platform and we started dating so that really makes me confident in him but it was too late to know I have been scammed after he successfully rips me and put me in a debt of $459k. The night I was to take poison and killed myself I came across ( ghostchampionwizard@gmail.com ) and they called me by WhatsApp after which I contacted them firstly, they talked to me and I was calm, less than 72 hours I have heavily restored back better than my normal life... all thanks to almighty great ghostchampionwizard Email: GHOSTCHAMPIONWIZARD (at) Gmail dot com Website: https://championhacker0.wixsite.com/my-site/about
Benito Fausto Level 0, United Kingdom
23 March 2022
👍
Carol BlankATMcard Level 0, Nigeria
4 March 2022
I want to testify