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 -> ValueMethods:
boolean equals(Object o) Parameters : o -> Object to which we want to compare Returns: true: if both objects are equals false: otherwise
K getKey() Parameters : ------------- Returns: K -> Returns the corresponding Key of a entry on which it is invokedException –
V getValue() Parameters : ------------- Returns: V -> Returns the corresponding value of a entry on which it is invoked
int hashcode() Parameters : ------------- Returns: int -> Returns the hash-code of entry on which it is invoked
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 invokedException :
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 valuesProgram 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4