Friday, August 12, 2022

Map.Entry Interface in Java

Map.Entry interface in Java denotes a map entry (key-value pair). Elements stored in any Map implementation like HashMap, LinkedHashMap are stored as an instance of Map.Entry. Entry interface is a nested interface with in a Map interface thus accessed as Map.Entry.

Entry interface

With in Map interface in Java, Entry interface is defined as below.

interface Entry<K,V> {
  K getKey();
  V getValue();

Methods in Java Map.Entry interface

Following methods are listed in the Map.Entry interface. Comparison related static methods were added in Java 8.

  • comparingByKey()- Returns a comparator that compares Map.Entry in natural order on key. Added in Java 8.
  • comparingByKey(Comparator<? super K> cmp)- Returns a comparator that compares Map.Entry by key using the given Comparator. Added in Java 8.
  • comparingByValue()- Returns a comparator that compares Map.Entry in natural order on value. Added in Java 8.
  • comparingByValue(Comparator<? super V> cmp)- Returns a comparator that compares Map.Entry by value using the given Comparator. Added in Java 8.
  • equals(Object o)- Compares the specified object with this entry for equality.
  • getKey()- Returns the key corresponding to this entry.
  • getValue()- Returns the value corresponding to this entry.
  • hashCode()- Returns the hash code value for this map entry.
  • setValue(V value)- Replaces the value corresponding to this entry with the specified value (optional operation).

Map.entrySet method

The Map.entrySet method defined in the Map interface returns a collection-view of the map, whose elements are of type Map.Entry. Getting the collection-view of the Map using entrySet() method is one of the best way to iterate a Map in Java.

Example code using entrySet() to get Map.Entry elements

Here is a Java example where we’ll get the “Collection view” of the map using the entrySet() method which returns a set view of the Map.Entry elements contained in this map.

public class HashMapSorting {
  public static void main(String[] args) {
    Map<String, String> langMap = new HashMap<String, String>();

    langMap.put("ENG", "English");
    langMap.put("NLD", "Dutch");
    langMap.put("ZHO", "Chinese");
    langMap.put("BEN", "Bengali");
    langMap.put("ZUL", "Zulu");
    langMap.put("FRE", "French");
    //Getting Map.Entry elements using entrySet()
    Set<Map.Entry<String, String>> langSet = langMap.entrySet();
    // Looping the set of Map.Entry values
    for(Map.Entry<String, String> entry : langSet){
        System.out.println("Key is " + entry.getKey() + " Value is " + entry.getValue());    


Key is ZHO Value is Chinese
Key is ZUL Value is Zulu
Key is NLD Value is Dutch
Key is FRE Value is French
Key is BEN Value is Bengali
Key is ENG Value is English

That's all for this topic Map.Entry Interface in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. How HashMap Works Internally in Java
  2. TreeMap in Java With Examples
  3. Difference Between HashMap And ConcurrentHashMap in Java
  4. How to Sort Elements in Different Order in TreeSet
  5. How to Remove Elements From an ArrayList in Java

You may also like-

  1. Difference Between Comparable and Comparator in Java
  2. Java Reflection API Tutorial
  3. Java Object Cloning - clone() Method
  4. Functional Interface Annotation in Java
  5. throws Keyword in Java Exception Handling
  6. String Vs StringBuffer Vs StringBuilder in Java
  7. How to Display Time in AM-PM Format in Java
  8. Word Count MapReduce Program in Hadoop