Map.Entry interface in Java with example

Last Updated : 22 Jan, 2026

The Map.Entry interface in Java provides methods to access and manipulate individual key-value pairs stored in a Map. By using this interface, we can efficiently traverse, read, and update map entries. The Map.Entry interface is generic and is defined in the java.util package.

Java
import java.util.HashMap;
import java.util.Map;
public class MapEntryDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "Java");
        map.put(2, "Python");
        map.put(3, "C++");
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
    }
}

Output
Key: 1, Value: Java
Key: 2, Value: Python
Key: 3, Value: C++

Explanation:

  • entrySet() returns all key-value pairs as a set of Map.Entry objects.
  • getKey() retrieves the key, getValue() retrieves the value.
  • This is the standard way to iterate through a map efficiently.

Syntax

interface Map.Entry<K, V>

Parameters

  • K ->The type of the key stored in this entry
  • V -> The type of the value associated with the key

Methods of Map.Entry Interface

1. equals(Object o)

equals(Object o) checks whether the current map entry is equal to the specified object.

Syntax

boolean equals(Object o)

  • Returns: true if both entries are equal false otherwise
Java
import java.util.HashMap;
import java.util.Map;
class GFG {
    public static void main (String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Apple", 10);
        map.put("Banana", 20);
        Map.Entry<String, Integer> e1 = map.entrySet().iterator().next();
        Map.Entry<String, Integer> e2 = map.entrySet().iterator().next();
        System.out.println(e1.equals(e2));
    }
}

Output
true

Explanation: Both e1 and e2 refer to the same map entry obtained from the iterator, so the equals() method returns true.

2. getKey()

getKey() returns the key associated with the current map entry.

Syntax

K getKey()

Java
import java.util.HashMap;
import java.util.Map;
public class GFG {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Java", 1);
        map.put("Python", 2);
        map.put("C++", 3);
        for (Map.Entry<String, Integer> e : map.entrySet()) {
            System.out.print(e.getKey()+" ");
        }
    }
}

Output
Java C++ Python 

Explanation:

  • entrySet() retrieves all key–value pairs from the map.
  • The enhanced for loop iterates over each Map.Entry.
  • getKey() extracts the key from the current entry.

3. getValue()

getValue() returns the value associated with the current map entry.

Syntax

V getValue()

Java
import java.util.HashMap;
import java.util.Map;
public class GFG {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Java", 1);
        map.put("Python", 2);
        map.put("C++", 3);
        for (Map.Entry<String, Integer> e : map.entrySet()) {
            System.out.print(e.getValue()+" ");
        }
    }
}

Explanation: getValue() fetches the value of the current entry.

4. hashCode()

hashCode() returns the hash code of the current map entry based on its key and value.

Syntax

int hashCode()

Java
import java.util.HashMap;
import java.util.Map;
public class GFG {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Java", 1);
        map.put("Python", 2);
        map.put("C++", 3);
        for (Map.Entry<String, Integer> e : map.entrySet()) {
            System.out.print(e.hashCode()+" ");
        }
    }
}

Explanation:

  • hashCode() returns a hash value based on the key and value.
  • The generated hash codes are printed to the console.

5. setValue(V value)

setValue(V value) updates the value associated with the current map entry.

Syntax

V setValue(V value)

  • Parameters: value-New value to be set
  • Returns:Old value of the entry
Java
import java.util.HashMap;
import java.util.Map;
public class GFG {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Java", 1000);
        map.put("Python", 2000);
        map.put("C++", 3000);
        for (Map.Entry<String, Integer> e : map.entrySet()) {
            e.setValue(e.getValue() + 1000);
        }
        System.out.println(map);
    }
}

Output
{Java=2000, C++=4000, Python=3000}

Explanation:

  • getValue() retrieves the current value of the entry.
  • setValue() updates the value directly in the original map.

6. entrySet()

entrySet() returns a set view of all key–value pairs present in the map.

Syntax

Set<Map.Entry<K,V>> entrySet()

  • Returns:Set of all map entries
Java
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class GFG {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Java", 1);
        map.put("Python", 2);
        map.put("C++", 3);
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        System.out.println(entries);
    }
}

Output
[Java=1, C++=3, Python=2]

Explanation:

  • entrySet() returns a Set containing all Map.Entry objects.
  • Each entry represents a key–value pair of the map.
  • Printing the set displays all entries in key=value format.
Comment