Open In App

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

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
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());
    }
}


Output

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


Output

1=Geeks
2=For
3=Geeks
1=Geeks
2=For
3=Geeks
1=Geeks
2=For
3=Geeks
keySet() 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.


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 can these methods with all the classes implementing
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 can these methods with all the classes implementing
4 min read
Map entrySet() method in Java with Examples
The java.util.Map.entrySet() method in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the map or we can create a new set and store the map elements into them. Syntax: map.entrySet() Parameters: The method does not take any parameter. Return Value: The method returns a set having same e
2 min read
Map keySet() Method in Java with Examples
This method is used to return a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. Syntax: Set keySet() Parameters: This method has no argument. Returns: This method returns a set containing keys of the specified map. Below programs show the implementation of int
2 min read
HashMap entrySet() Method in Java
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
2 min read
TreeMap entrySet() Method in Java
The java.util.TreeMap.entrySet() method in Java is used to create a set out of the same elements contained in the treemap. It basically returns a set view of the treemap or we can create a new set and store the map elements into them. Syntax: tree_map.entrySet() Parameters: The method does not take any parameter. Return Value: The method returns a
2 min read
EnumMap entrySet() Method in Java
The Java.util.EnumMap.entrySet() method in Java used to create a set out of the elements contained in the EnumMap. It basically returns a set view of the enum map. Syntax: enum_map.entrySet() Parameter: The method does not take any parameters. Return Value: The method returns the set view of mappings contained in this enum_map. Below programs illus
2 min read
WeakHashMap entrySet() Method in Java
The java.util.WeakHashMap.entrySet() method in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the weak hash map or we can create a new set and store the map elements into them. Syntax: weak_hash_map.entrySet() Parameters: The method does not take any parameter. Return Value: The method
2 min read
IdentityHashMap entrySet() Method in Java
The java.util.IdentityHashMap.entrySet() method in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the identity hash map or we can create a new set and store the map elements into them. Syntax: Identity_HashMap.entrySet() Parameters: The method does not take any parameter. Return Value:
2 min read
SortedMap entrySet() method in Java with Examples
The entrySet() method of SortedMap interface in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the map or creates a new set and store the map elements into them. Syntax: SortedMap.entrySet() Parameters: The method does not take any parameter. Return Value: The method returns a set havi
2 min read