Difference Between keySet() and entrySet() Method in Java Map
Last Updated :
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
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)
{
Map<Integer, String> map = new HashMap<>();
map.put( 1 , "Geeks" );
map.put( 2 , "For" );
map.put( 3 , "Geeks" );
Iterator<Integer> itr = map.keySet().iterator();
while (itr.hasNext())
{
System.out.print(itr.next() + " " );
}
System.out.println();
for (Integer key : map.keySet()) {
System.out.print(key + " " );
}
System.out.println();
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
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)
{
Map<Integer, String> map = new HashMap<>();
map.put( 1 , "Geeks" );
map.put( 2 , "For" );
map.put( 3 , "Geeks" );
for (Map.Entry<Integer, String> entry :map.entrySet())
{
Integer key = (Integer)entry.getKey();
String value = entry.getValue();
System.out.println(key + "=" + value);
}
Iterator<Map.Entry<Integer, String> > itr = map.entrySet().iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
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. |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...