Difference Between keySet() and entrySet() Method in Java Map

02 Jun, 2022
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());    }}
1 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);    }}
1=Geeks
2=For
3=Geeks
1=Geeks
2=For
3=Geeks
1=Geeks
2=For
3=Geeks
Its syntax is -:
hashmap.keySet()
Its syntax is -:
hashmap.entrySet()
avijitpaul16july
mayank007rawa
Java-Collections
java-map
Picked
Technical Scripter 2020
Difference Between
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Compile-time and Run-time Polymorphism in Java
Similarities and Difference between Java and C++
Difference between DELETE and TRUNCATE
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java