VOOZH about

URL: https://www.geeksforgeeks.org/java/hashmap-entryset-method-in-java/

⇱ HashMap entrySet() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

HashMap entrySet() Method in Java

Last Updated : 6 Aug, 2025

The entrySet() method of the HashMap class in Java is used to create a set view of the mappings contained in the HashMap. This method allows us to iterate over the key-value pairs in the map or convert them into a set.

Example 1: Here, we will use the entrySet() method to view the mappings in a HashMap.


Output
Initial Mappings: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=for}
The set is: [20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=for]

Explanation: In the above example, the entrySet() method provides a Set view of the mappings in the HashMap. Each element in the set is a Map.Entry object that represents a key-value pair.

Syntax of HashMap entrySet() Method

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

  • Parameters: The method does not take any parameter.
  • Return Value: The method returns a set having same elements as the hash map.

Example 2: Here, we will use the entrySet() method with a HashMap having String keys and Integer values.


Output
Initial Mappings: {Geeks=20, for=15, You=30, Welcomes=25}
The set is: [Geeks=20, for=15, You=30, Welcomes=25]

Explanation: In the above example, the key "Geeks" is updated with the latest value 20. The entrySet() method displays all key-value pairs as a Set.

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.

Comment