Map interface keySet(), values(), entrySet() methods in Java
In this post, we will explore the three methods keySet(), values(), and entrySet() of the Map interface in Java. These methods are used to retrieve a set of keys, a collection of values, or a set of key-value mappings from the Map, respectively.
1. Overview of keySet(), values(), and entrySet() methods
The java.util.Map interface provides three methods keySet(), values(), and entrySet(), which allow a Map’s contents to be viewed as a set of keys, collection of values, or set of key-value mappings, respectively. The Map backs the collection returned by these methods, so any changes to the Map are reflected in the collection and vice-versa.
Let’s see the syntax and return type of these methods:
- The keySet() method returns a
Setview of the keys contained in theMap. The method does not take any parameters and has a return typeSet<K>, whereKis the type of keys in theMap. - The
values()method returns aCollectionview of the values contained in theMap. The method does not take any parameters and has a return typeCollection<V>, whereVis the type of values in theMap. - The entrySet() method returns a
Setview of the key-value mappings contained in theMap. The method does not take any parameters and has a return typeSet<Map.Entry<K,V>>, whereKis the type of keys andVis the type of values in theMap.Map.Entryis an interface that represents a key-value pair.
Let’s see an example of how to create and use these methods on a HashMap:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; class Main { public static void main(String[] args) { // Create and initialize a HashMap HashMap<String, Integer> map = new HashMap<>(); map.put("one", 1); map.put("two", 2); map.put("three", 3); // Get the keySet from the map Set<String> keys = map.keySet(); // Print the keySet System.out.println("Keys: " + keys); // [one, two, three] // Get the values from the map Collection<Integer> values = map.values(); // Print the values System.out.println("Values: " + values); // [1, 2, 3] // Get the entrySet from the map Set<Map.Entry<String, Integer>> entries = map.entrySet(); // Print the entrySet System.out.println("Entries: " + entries); // [one=1, two=2, three=3] } } |
2. Differences between keySet(), values(), and entrySet() methods
The functionality of keySet(), values(), and entrySet() methods are similar. They all return a collection view of some aspect of the Map. However, they also have some differences and trade-offs that we must understand:
- The
keySet()method returns a Set view of all the keys present in theMap, i.e., it returns a set of keys. Thevalues()method returns a Collection view of all the values present in theMap, i.e., it returns a collection of values. TheentrySet()method returns a Set view of all the mappings present in theMap, i.e., it returns a set of key-value pairs. - The
keySet()method can be used to iterate over all the keys in the Map and perform an action based on each key. Thevalues()method can be used to iterate over all the values in theMapand perform an action based on each value. TheentrySet()method can be used to iterate over all the entries in the Map and perform an action based on each key-value pair. - The
keySet()method does not allow duplicate keys in the Set, as theMapdoes not allow duplicate keys. Thevalues()method allows duplicate values in the Collection, as theMapallows duplicate values. TheentrySet()method does not allow duplicate entries in the Set, as theMapdoes not allow duplicate entries.
3. How to use keySet(), values(), and entrySet() methods correctly
Here are some general recommendations on how to use these methods correctly:
- Use
keySet()method when we only need to access or modify the keys of theMap, and we do not care about the values or the entries. For example, we can usekeySet()method to check if a key exists in theMap, to remove a key from theMap, or to iterate over all the keys in theMap. - Use
values()method when we only need to access or modify the values of theMap, and we do not care about the keys or the entries. For example, we can usevalues()method to check if a value exists in theMap, to remove a value from theMap, or to iterate over all the values in theMap. - Use
entrySet()method when we need to access or modify both the keys and the values of theMap, or when we need to access or modify the entries of theMap. For example, we can useentrySet()method to check if an entry exists in theMap, to remove an entry from theMap, or to iterate over all the entries in theMap.
That’s all about the keySet(), values() and entrySet() method in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)