Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is HashMap entrySet() in Java?

Samia Ishaque

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

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());
}
}

RELATED TAGS

java

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring