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 Set view of the keys contained in the Map. The method does not take any parameters and has a return type Set<K>, where K is the type of keys in the Map.
  • The values() method returns a Collection view of the values contained in the Map. The method does not take any parameters and has a return type Collection<V>, where V is the type of values in the Map.
  • The entrySet() method returns a Set view of the key-value mappings contained in the Map. The method does not take any parameters and has a return type Set<Map.Entry<K,V>>, where K is the type of keys and V is the type of values in the Map. Map.Entry is an interface that represents a key-value pair.

Let’s see an example of how to create and use these methods on a HashMap:

Download  Run Code

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 the Map, i.e., it returns a set of keys. The values() method returns a Collection view of all the values present in the Map, i.e., it returns a collection of values. The entrySet() method returns a Set view of all the mappings present in the Map, 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. The values() method can be used to iterate over all the values in the Map and perform an action based on each value. The entrySet() 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 the Map does not allow duplicate keys. The values() method allows duplicate values in the Collection, as the Map allows duplicate values. The entrySet() method does not allow duplicate entries in the Set, as the Map does 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 the Map, and we do not care about the values or the entries. For example, we can use keySet() method to check if a key exists in the Map, to remove a key from the Map, or to iterate over all the keys in the Map.
  • Use values() method when we only need to access or modify the values of the Map, and we do not care about the keys or the entries. For example, we can use values() method to check if a value exists in the Map, to remove a value from the Map, or to iterate over all the values in the Map.
  • Use entrySet() method when we need to access or modify both the keys and the values of the Map, or when we need to access or modify the entries of the Map. For example, we can use entrySet() method to check if an entry exists in the Map, to remove an entry from the Map, or to iterate over all the entries in the Map.

That’s all about the keySet(), values() and entrySet() method in Java.