The Wayback Machine - https://web.archive.org/web/20240913203717/https://www.geeksforgeeks.org/map-entry-interface-java-example/
Open In App

Map.Entry interface in Java with example

Last Updated : 02 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

  1. 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
    
  2. 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.
  3. 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
    
  4. 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
    
  5. 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 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


Similar Reads

How to find the Entry with largest Value in a Java Map
Given a map in Java, the task is to find out the entry in this map with the highest value. Illustration: Input : Map = {ABC = 10, DEF = 30, XYZ = 20} Output : DEF = 30Input : Map = {1 = 40, 2 = 30, 3 = 60} Output : 3 = 60 Methods: There can be several approaches to achieve the goal that are listed as follows: Simple iterative approach via for each
4 min read
How to find the Entry with largest Key in a Java Map
Given a map in Java, the task is to find out the entry in this map with the highest key. Examples: Input: Map = {ABC = 10, DEF = 30, XYZ = 20} Output: XYZ = 20 Input: Map = {1 = 40, 2 = 30, 3 = 60} Output: 3 = 60 Approach Iterate the map entry by entryfor (Map.Entry entry : map.entrySet()) { // Operations }Store the first entry in a reference varia
2 min read
Why to Use Comparator Interface Rather than Comparable Interface in Java?
In Java, the Comparable and Comparator interfaces are used to sort collections of objects based on certain criteria. The Comparable interface is used to define the natural ordering of an object, whereas the Comparator interface is used to define custom ordering criteria for an object. Here are some reasons why you might want to use the Comparator i
8 min read
Java 8 | DoubleToIntFunction Interface in Java with Example
The DoubleToIntFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a double-valued argument and gives an int-valued result. The lambda expression assigned to an object of DoubleToIntFunction type is used to define
1 min read
Map Interface in Java
Let us start with a simple Java code snippet that demonstrates how to create and use a Map in Java. [GFGTABS] Java import java.util.HashMap; import java.util.Map; public class MapCreationExample { public static void main(String[] args) { // Create a Map using HashMap Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;(); //here, we can write all o
12 min read
Java 8 | ObjLongConsumer Interface with Example
The ObjLongConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in two arguments and produces a result. However these kind of functions don't return any value. Hence this functional interface takes in one generic namel
2 min read
Java 8 | ObjIntConsumer Interface with Example
The ObjIntConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in two arguments and produces a result. However these kind of functions don't return any value. Hence this functional interface takes in one generic namely
2 min read
Java 8 | ObjDoubleConsumer Interface with Example
The ObjDoubleConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in two arguments and produces a result. However these kind of functions don't return any value. Hence this functional interface takes in one generic nam
2 min read
NavigableMap Interface in Java with Example
The NavigableMap interface is a member of the Java Collection Framework. It belongs to java.util package and It is an extension of SortedMap which provides convenient navigation methods like lowerKey, floorKey, ceilingKey and higherKey, and along with this popular navigation method. It also provide ways to create a Sub Map from existing Map in Java
11 min read
Java Native Interface with Example
In the Programming World, it is often seen in the comparison of Java vs C/C++. Although the comparison doesn't make any such sense, as both the languages have their Pros and Cons, but what if we can use multiple languages in a single Program? In this article, we will learn How to use JNI(Java Native Interface) to run multiple languages in a single
4 min read
Deque interface in Java with Example
Let us start with a simple Java code snippet demonstrating creating a Deque in Java. [GFGTABS] Java import java.util.ArrayDeque; import java.util.Deque; public class DequeCreation { public static void main(String args[]) { // Create a Deque of Strings Deque&lt;String&gt; deque = new ArrayDeque&lt;&gt;(); // here, we can write all the operations in
10 min read
Initialize a static Map using Java 9 Map.of()
In this article, a static map is created and initialised in Java using Java 9. Static Map in Java A static map is a map which is defined as static. It means that the map becomes a class member and can be easily used using class. Java 9 feature - Map.of() method In Java 9, Map.of() was introduced which is a convenient way to create instances of Map
2 min read
Map clear() method in Java with Example
The java.util.Map.clear() method in Java is used to clear and remove all of the elements or mappings from a specified Map collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the working of java.util.Map.clear() Method: Progr
2 min read
Spring - RowMapper Interface with Example
Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier t
6 min read
Java 8 | IntToDoubleFunction Interface in Java with Examples
The IntToDoubleFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an int-valued argument and gives a double-valued result. The lambda expression assigned to an object of IntToDoubleFunction type is used to define
1 min read
Java 8 | DoubleToLongFunction Interface in Java with Examples
The DoubleToLongFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a double-valued argument and gives an long-valued result. The lambda expression assigned to an object of DoubleToLongFunction type is used to defi
1 min read
Java.util.function.BiPredicate interface in Java with Examples
The BiPredicate&lt;T, V&gt; interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on two objects and returns a predicate value based on that condition. It is a functional interface and thus can be used in lambda expression also. public interface BiPredicate&lt;T, V&gt; Methods: test(): This functio
2 min read
Java.util.function.DoublePredicate interface in Java with Examples
The DoublePredicate interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on a Double object and returns a predicate value based on a condition. It is a functional interface and thus can be used in lambda expression also. public interface DoublePredicate Methods test(): This function evaluates a co
2 min read
Java.util.function.LongPredicate interface in Java with Examples
The LongPredicate interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on a long value and returns a predicate value based on a condition. It is a functional interface and thus can be used in lambda expression also. public interface LongPredicate Methods test(): This function evaluates a condition
2 min read
Java.util.function.IntPredicate interface in Java with Examples
The IntPredicate interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on an integer value and returns a predicate value based on a condition. It is a functional interface and thus can be used in lambda expression also. public interface IntPredicate Methods: test(): This function evaluates a condit
2 min read
java.net.FileNameMap Interface in Java
java.net.FileNameMap is a Java Interface. FileNameMap interface is a part of the java.net package. FileNameMap provides a mechanism to map between a file name and a MIME type string. We can use FileNameMap getContentTypeFor method to get the MIME type of the specified file. Syntax: public interface FileNameMapMethod of java.net.FileNameMap Interfac
2 min read
JUnit5 - Map Assertions With AssertJ with Example Maven Project
In this article, by seeing a sample project that involves a few characters in the "friends" web series and checking their names against correct professions as fun. For storing names and their professions, we need a "HashMap" facility. In that let us store their names and professions. With that, we need to check all the features applicable to JUnit5
4 min read
Remove an Entry using key from HashMap while Iterating over it
Given a HashMap and a key in Java, the task is to remove an entry from this HashMap using the key, while iterating over it. Examples: Input: HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}, key = 2 Output: {1=Geeks, 3=GeeksForGeeks} Input: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, key = 3 Output: {1=G, 2=e, 4=k, 5=s} Using Java 7 and before: Get the Hash
3 min read
Remove an Entry using value from HashMap while Iterating over it
Given a HashMap and a value in Java, the task is to remove an entry from this HashMap using the value, while iterating over it. Examples: Input: HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}, value = "ForGeeks" Output: {1=Geeks, 3=GeeksForGeeks} Input: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, value = k Output: {1=G, 2=e, 3=e, 5=s} Using Java 7 and bef
3 min read
ConcurrentMap Interface in java
ConcurrentMap is an interface and it is a member of the Java Collections Framework, which is introduced in JDK 1.5 represents a Map that is capable of handling concurrent access to it without affecting the consistency of entries in a map. ConcurrentMap interface present in java.util.concurrent package. It provides some extra methods apart from what
9 min read
Enumeration Interface In Java
java.util.Enumeration interface is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable( like Stack, Vector, HashTable etc.) in a forward direction only and not in the backward direction. This interface has been superceded by an iterator. The Enumeration Interface defines the functions b
3 min read
Java | Implementing Iterator and Iterable Interface
Iterators are used in Collection framework in Java to retrieve elements one by one. For more details and introduction related to this, see this link. Why it is needed to implement Iterable interface? Every class that implements Iterable interface appropriately, can be used in the enhanced For loop (for-each loop). The need to implement the Iterator
4 min read
Runnable interface in Java
java.lang.Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread. There are two ways to start a new Thread - Subclass Thread and implement Runnable. There is no need of subclassing a Thread when a task can be done by overriding only run() method of Runnable. Steps to create a new thread
3 min read
Static method in Interface in Java
Static Methods in Interface are those methods, which are defined in the interface with the keyword static. Unlike other methods in Interface, these static methods contain the complete definition of the function and since the definition is complete and the method is static, therefore these methods cannot be overridden or changed in the implementatio
2 min read
ToLongFunction Interface in Java with Examples
The ToLongFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an argument of type T and produces a long-valued result. This functional interface takes in only one generic, namely:- T: denotes the type of the input
1 min read
Article Tags :
Practice Tags :