Map.Entry interface in Java with example
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 –
- IllegalSateException 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 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 channge 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 channge of value = 1 - Bedroom 25000 After change of value = 1 - Bedroom 59475 Before channge of value = 2 - Bedroom 50000 After change of value = 2 - Bedroom 51650 Before channge of value = 3 - Bedroom 75000 After change of value = 3 - Bedroom 95200 Before channge of value = 1 - Bedroom - hall 65000 After change of value = 1 - Bedroom - hall 74112 Before channge of value = 2 - Bedroom - hall 85000 After change of value = 2 - Bedroom - hall 41490 Before channge of value = 3 - Bedroom - hall 105000 After change of value = 3 - Bedroom - hall 10902
This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Java.util.BitSet class methods in Java with Examples | Set 2
- Shadowing of static functions in Java
- How does default virtual behavior differ in C++ and Java ?
- How are Java objects stored in memory?
- How are parameters passed in Java?
- Are static local variables allowed in Java?
- final variables in Java
- Default constructor in Java
- Assigning values to static final variables in Java
- Comparison of Exception Handling in C++ and Java
- Does Java support goto?
- Arrays in Java
- Inheritance and constructors in Java
- More restrictive access to a derived class method in Java
- Comparison of static keyword in C++ and Java




