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.
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]
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
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. |