The “Map.Entry” Interface in Java provides an effective approach to work with the entries allocated as “key-value” pairs in a container. These pairs are essential in various data-handling situations. In such cases, this interface comes into effect by invoking these records individually or updating them via reference effectively.
This tutorial will demonstrate working with the “Map.Entry” interface in Java.
What is Map.Entry Interface in Java?
The “Map.Entry” interface in Java is defined in the “java.util” package that provides various methods to access the entry in the Map. By gaining access to the entry of the Map, we can easily manipulate them.
The “entrySet()” method defined by the Map interface gives a Set comprising the entries of a map where all the set elements correspond to a Map.Entry object.
Methods of the Map.Entry Interface
Method | Functionality |
---|---|
getKey() | Displays the map entry’s key. |
getValue() | Displays the map entry’s value. |
hashCode() | It gives the map entry’s hash code. |
equals(obj) | This method gives true if obj i.e., object is a Map.Entry whose key and value are equivalent to that of the accessing object. |
setValue(Object x) | Sets the value for this map entry to x. Exceptions Thrown by this MethodThe “ClassCastException” is thrown if “x” is not the correct type for the map.A “NullPointerException” is returned if “x” is null and the map restricts the null keys.The “UnsupportedOperationException” is returned if the map cannot be altered/changed. |
Example: Working of the Map.Entry Interface in Java
The following code explains the working of the discussed interface:
import java.util.*;
import java.util.Iterator;
public class Mapentry {
public static void main(String args[]) {
HashMap<String, Integer> values = new HashMap<String, Integer>();
values.put("Liam", 100);
values.put("Jacob", 200);
values.put("John", 300);
Set x = values.entrySet();
Iterator itr = x.iterator();
while(itr.hasNext()) {
Map.Entry y = (Map.Entry)itr.next();
System.out.print(y.getKey() + "-> ");
System.out.println(y.getValue());
}
System.out.println();
Integer z = values.get("Liam");
values.put("Liam", z + 150);
System.out.println("Liam's updated value -> " + values.get("Liam"));
}}
In this block of code:
- First of all, import the “java.util.*” and “java.util.Iterator” packages to access all the functionalities in the “java.util” package and work with the “Iterator” class, respectively.
- Now, create a HashMap comprising the “keys” as String and “values” as Integers.
- Also, add the given “key-values” pairs in the HashMap accordingly.
- After that, associate the “entrySet()” method with the HashMap to return a Set comprising the entries of a map.
- In the next step, iterate through the set via the “iterator()” method.
- Apply the combined “hasNext()” and “next()” methods to check for the next elements and invoke them as “keys” and “values” individually via the “getKey()” and “getValue()” methods, thereby refraining from the limitation(s).
- Lastly, invoke the value against the corresponding key i.e., “Liam” and update it via the combined “get()” and “put()” methods and display it.
Output

In this outcome, it can be verified that the ArrayList is iterated appropriately and the target value against the key is updated accordingly.
Conclusion
The “Map.Entry” interface in Java is defined in the “java.util” package and comprises various methods to access the entry in the Map. It is a vital functionality to invoke map entries effectively. This blog demonstrated using the “Map.Entry” interface in Java.