Map.Entry interface in Java with example
Last Updated :
02 Nov, 2022
Map.Entry interface in Java provides certain methods to access the entry in the Map. By gaining access to the entry of the Map we can easily manipulate them. Map.Entry is a generic and is defined in the java.util package.
Declaration :
Interface Map.Entry
k -> Key
V -> Value
Methods:
- equals (Object o) - It compares the object (invoking object) with the Object o for equality.
Syntax :
boolean equals(Object o)
Parameters :
o -> Object to which we want to compare
Returns:
true: if both objects are equals
false: otherwise
- K getKey() - Returns the key for the corresponding map entry.
Syntax :
K getKey()
Parameters :
-------------
Returns:
K -> Returns the corresponding Key of a entry on which it is invoked
Exception –
- IllegalStateException is thrown when entry has been removed from the map.
- V getValue() - Returns the value for the corresponding map entry.
Syntax :
V getValue()
Parameters :
-------------
Returns:
V -> Returns the corresponding value of a entry on which it is invoked
- int hashcode() - Returns the hashcode for the corresponding map entry.
Syntax :
int hashcode()
Parameters :
-------------
Returns:
int -> Returns the hash-code of entry on which it is invoked
- V setValue(V v) – Sets the value of the map with specified value v
V setValue(V v)
Parameters :
v -> Value which was earlier stored in the entry on which it is invoked
Returns:
int -> Returns the hash-code of a entry on which it is invoked
Exception :
- ClassCastException is thrown if the class of the value ‘v’ is not a correct type for map.
- NullPointerException is thrown if ‘null’ is stored in ‘v’, and ‘null ’ is not supported by map.
- UnsupportedOperationException is thrown if we cannot manipulate the map or the put operation is not supported by the map.
- IllegalArgumetException is thrown If there is some problem with the argument i.e v
- IllegalStateException is thrown when entry has been removed from the map
Set<Map.Entry> entrySet() - Returns the Set view of the entire map.
Note : This is not a method of Map.entry interface but it is discussed here because this method is useful while working with Map.Entry interface.
Set<Map.Entry> entrySet()
Parameters :
---------------
Returns:
Set<Map.Entry> ->: Returns a Set containing the Map.Entry values
Program below demonstrate the working of Map.Entry:
Java highjlight=
// Java Program to demonstrate the
// methods of Map.Entry
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class GFG
{
public static void main(String[] args)
{
// Create a LinkedHashMap
LinkedHashMap<String,Integer> m =
new LinkedHashMap<String, Integer>();
m.put("1 - Bedroom" , 25000);
m.put("2 - Bedroom" , 50000);
m.put("3 - Bedroom" , 75000);
m.put("1 - Bedroom - hall", 65000);
m.put("2 - Bedroom - hall", 85000);
m.put("3 - Bedroom - hall", 105000);
// Using entrySet() to get the entry's of the map
Set<Map.Entry<String,Integer>> s = m.entrySet();
for (Map.Entry<String, Integer> it: s)
{
// Using the getKey to get key of the it element
// Using the getValue to get value of the it element
System.out.println("Before change of value = " +
it.getKey() + " " + it.getValue());
// Changing the value of 1 - Bedroom.
double getRandom = Math.random() * 100000;
int getRoundoff = (int) Math.round(getRandom);
// Using setValue to change the value of the
// map element
it.setValue(getRoundoff);
System.out.println("After change of value = " +
it.getKey() + " " + it.getValue());
}
}
}
Output:
Before change of value = 1 - Bedroom 25000
After change of value = 1 - Bedroom 59475
Before change of value = 2 - Bedroom 50000
After change of value = 2 - Bedroom 51650
Before change of value = 3 - Bedroom 75000
After change of value = 3 - Bedroom 95200
Before change of value = 1 - Bedroom - hall 65000
After change of value = 1 - Bedroom - hall 74112
Before change of value = 2 - Bedroom - hall 85000
After change of value = 2 - Bedroom - hall 41490
Before change of value = 3 - Bedroom - hall 105000
After change of value = 3 - Bedroom - hall 10902