Difference Between keySet() and entrySet() Method in Java Map Last Updated : 02 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use these methods with all the classes implementing the map interface like TreeMap, HashMap, and LinkedHashMap. keySet() Method: The java.util.HashMap.keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys or we can create a new set and store the key elements in them. Syntax: hash_map.keySet() Parameters: The method does not take any parameter. Return Value: The method returns a set having the keys of the hash map. Java // Java program demonstrating use of keySet() import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.stream.Stream; class GFG { public static void main(String[] args) { // making map of Integer keys and String values Map<Integer, String> map = new HashMap<>(); // adding the key-value pairs to map map.put(1, "Geeks"); map.put(2, "For"); map.put(3, "Geeks"); // below are the different and simple ways out of // many to iterate over the keySet() // iterating the keySet() using iterator Iterator<Integer> itr = map.keySet().iterator(); while (itr.hasNext()) { System.out.print(itr.next() + " "); } System.out.println(); // iterating the keySet() using for loop for (Integer key : map.keySet()) { System.out.print(key + " "); } System.out.println(); // iterating over the keySet() by converting the map // to the string System.out.println(map.keySet().toString()); } } Output1 2 3 1 2 3 [1, 2, 3] entrySet() Method: The java.util.HashMap.entrySet() method in Java is used to create a set out of the same elements contained in the hash map. It basically returns a set view of the hash map or we can create a new set and store the map elements into them. Syntax: hash_map.entrySet() Parameters: The method does not take any parameter. Return Value: The method returns a set having same elements as the hash map. Java // Java program demonstrating use of entrySet() import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.stream.Stream; class GFG { public static void main(String[] args) { // making map of Integer keys and String values Map<Integer, String> map = new HashMap<>(); // adding the key-value pairs to map map.put(1, "Geeks"); map.put(2, "For"); map.put(3, "Geeks"); // below are the different and simple ways out of // many to iterate over the entrySet() // iterating the key value pair using for each loop for (Map.Entry<Integer, String> entry :map.entrySet()) { Integer key = (Integer)entry.getKey(); String value = entry.getValue(); System.out.println(key + "=" + value); } // iterating the key-value pairs using iterator Iterator<Map.Entry<Integer, String> > itr = map.entrySet().iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } // iterating the key-value pairs using Stream.of() // method Stream.of(map.entrySet().toArray()) .forEach(System.out::println); } } Output1=Geeks 2=For 3=Geeks 1=Geeks 2=For 3=Geeks 1=Geeks 2=For 3=GeekskeySet()entrySet()This method returns the Set view of all the keys present in the map, ie it returns a set of keys.This method returns the Set view of all the mappings present in the map, ie it returns a set of key, value pairs.If any changes happen to the map, then they can be observed in the set also,as set is backed up by the map. For entrySet() method also, If any changes happen to the map, then they can be observed in the set also,as set is backed up by the map. If iterating through all the pairs of maps using keySet(), then the performance of keySet() is poorer as compared to entrySet(), as for each key, we have to access its corresponding value by using get() function.When Iterating through all the pairs of the map using entrySet(), then the performance of entrySet() is much better as compared to keySet(). Its syntax is -: hashmap.keySet() Its syntax is -: hashmap.entrySet() It does not take any parameters.It has no exceptions. Comment More infoAdvertise with us Next Article Difference Between keySet() and entrySet() Method in Java Map L lavishgarg26 Follow Improve Article Tags : Java Technical Scripter Difference Between Technical Scripter 2020 Java-Collections java-map +2 More Practice Tags : JavaJava-Collections Similar Reads Difference Between value() vs entrySet() Method in Java Map Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use ca 5 min read Difference Between keySet() vs value() Method in Java Map Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use ca 4 min read Difference between List, Set and Map in Java List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos 4 min read Difference Between EnumSet and TreeSet in Java EnumSet and TreeSet both are the classes defined inside the collection framework. But there are few differences exists between them. In this article, we have tried to cover all these differences between them. 1. EnumSet: EnumSet is a specialized implementation of the Set interface for enumeration ty 3 min read Difference between TreeMap and TreeSet in Java TreeSet is mainly an implementation of SortedSet in java where duplication is not allowed and objects are stored in sorted and ascending order. Some important features of the TreeSet are: In TreeSet duplicate values are not allowed because it implements the SortedSet interface.Objects in a TreeSet a 2 min read Difference Between List and Set in Java The List interface allows storing the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values are allowed to store. List preserves the insertion order, it allows positional access and insertion of elements. Declaration: public abstr 2 min read Difference Between Hashtable and Synchronized Map in Java The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a Hashtable, the objects used as keys must implement the hashCode method and also the equals method. Features of Hashtable: 3 min read Difference Between TreeSet and SortedSet in Java TreeSet is one of the implementations of the Navigable sub-interface. It is underlying data structure is a red-black tree. The elements are stored in ascending order and more methods are available in TreeSet compare to SortedSet. We can also change the sorting parameter using a Comparator. For examp 3 min read Difference Between LinkedList and LinkedHashSet in Java In this article you will learn difference between LinkedList and LinkedHashSet in java. Prerequisite: LinkedList : LinkedHashSet LinkedList class implements the List and Deque interface and extends from AbstractSequentialList class. LinkedList class uses doubly linked list to store the elements. It 3 min read Difference Between EnumMap and EnumSet in Java EnumMap and EnumSet both are the classes defined inside the java collection. In this article, we will learn the differences between EnumMap and EnumSet. EnumMap is the specialized implementation of the Map interface and the EnumSet is the specialized implementation of the Set interface. There are so 3 min read Like