What is HashMap entrySet() in Java?
Overview
The entrySet function in HashMap is used to form and display a set of HashMap keys and values. HashMap is defined in the util package in Java, so you must import the util package before using the entrySet function, as shown below:
import java.util.*;
The syntax for entrySet is as shown below:
hashmapName.entrySet()
Parameters
The entrySet function takes no mandatory arguments as input.
Return value
The entrySet function returns a set version of HashMap keys and values.
Examples
The example below will help you understand the entrySet function better. We first define a HashMap and enter keys using the put function. Then, we print the set version of the HashMap, using the function entrySet:
import java.util.*;class HelloWorld {public static void main( String args[] ) {HashMap<String, Integer> hash = new HashMap<String, Integer>();hash.put("Samia", 22);hash.put("Sana", 21);hash.put("Ali", 21);System.out.println("Size of Map: " + hash.size());System.out.println("Set version of the HashMap: " + hash.entrySet());}}
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved