Java Hashmap entrySet() Method
What is a Hashmap?
Hashmap class is a part of java.util package. In hashmap key/value pairs can be stored , but no duplicate key is allowed.If a duplicate key is inserted it will replace the present value with the new value. Many built-in methods are there in hashmap class which makes it easy to perform operations on them, like insertion , deletion and many more. One of those methods is Java Hashmap entrySet() Method.
To know more about Hashmap entrySet() Method read the complete article.
Java Hashmap entrySet() Method
The hashmap entrySet() method returns a set of elements present in the hashmap. It is used make a set of elements in the hashmap. Below in this page you can find it’s syntax, return values, parameters with detailed examples.
Syntax :
new_hashmap.entrySet();
Return values :
This method returns a set of elements present in the hashmap.
Hashmap entrySet() Method Example
Example :
import java.util.*; public class Main { public static void main (String[]args) { // Creating an empty HashMap HashMap < String, Integer > hash_map_1 = new HashMap < String, Integer > (); // Mapping string values to int keys in a empty hashmap hash_map_1.put ("Maths", 80); hash_map_1.put ("English", 75); hash_map_1.put ("Hindi", 85); hash_map_1.put ("Science", 90); System.out.println("Original hashmap : " + hash_map_1); //printing the set of elements present in the hashmap System.out.println("Set of elements present in the hashmap : "+hash_map_1.entrySet()); } }
Output :
Original hashmap : {Maths=80, English=75, Science=90, Hindi=85} Set of elements present in the hashmap : [Maths=80, English=75, Science=90, Hindi=85]
Now entrySet() method is called which returns a set view of elements present in the hashmap.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others