CodeGym /Java Blog /Java Collections /How to use entrySet() method in Java
Author
Volodymyr Portianko
Java Engineer at Playtika

How to use entrySet() method in Java

Published in the Java Collections 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.

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 (7)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Jasmine Tom Level 0, United States
9 November 2022
BITCOIN RECOVERY I was actually fooled and scammed by someone I trusted with my funds through a transaction we did and I feel so disappointed and hurt knowing that someone can steal from you without remorse after trusting them, so I started searching for help legally to recover my stolen funds and came across a lot of Testimonials about Mr Morris Gray, an agent who helps in recovery lost funds, which I can tell has helped so many people who had contacted him regarding such issues and without a questionable doubt their funds was returned back to their wallet in a very short space of time, it took the expert 48hours to help me recover my funds and the best part of it all was that the scammers was actually located and arrested by local authorities in his region which was very relieving. Hope this helps as many people who have lost their hard earn money to scammers out of trust, and you can reach him through the link below for help to recover your scammed funds and thank me later. Email Address: MorrisGray830@gmail .com WhatsApp: + 1 (607) 698-0239.
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
Carol BlankATMcard Level 0, Nigeria
4 March 2022
I want to testify