Map.Entry in Java


Java Map-EntryViews 3176

Map.Entry in Java is an interface in Java that enables us to access the entries within a map. It is a nested interface within the Map interface and hence we use it as Map.Entry. In this tutorial, we will discuss the Map.Entry Interface in Java with examples using its various methods.

We can use the Map.Entry interface to navigate through any Map elements like HashMap, TreeMap, etc and retrieve the keys and values separately for each entry. We can also update the values individually for each key in an entrySet.

Map.Entry interface Declaration

Below is the declaration of the Map.Entry interface in Java

interface Entry {
     K getKey();
     V getValue();
     ...
     ...
}

Methods

The entrySet() method of the Map interface returns a set object. Using this we can iterate through various elements in the map. To access the entrySet values, we can use the Map.Entry interface methods as below:

MethodsDescription
boolean equals(Object o)Compares if two object entries are equal
Object getKey()Returns the key corresponding to the entry
Object getValue()Returns the value corresponding to the entry
Object setValue(Object value)Replaces the old value with the specified new value
int hashCode()Returns the hashcode value of the corresponding entry

Java Map.Entry examples

Now, let’s see various Map.Entry interface in Java with examples using its methods.

Example: Using getKey() and getValue() methods

This example shows how to retrieve the keys and values of the corresponding entries using the getKey() and getValue() methods. First, we create a TreeMap and elements using the put() method. Next, we create an entrySet and use the iterator method to gain access to the TreeMap iterator. Finally, during iteration, we can convert each entrySet into Map.Entry object and access the individual key and value separately.

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MapEntryDemo {

  public static void main(String[] args) {
    TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
    tm.put(100, "Akash");
    tm.put(200, "Bharat");
    tm.put(300, "Chetan");
    tm.put(400, "Dev");
    tm.put(500, "Eshwar");
    
    Set s = tm.entrySet();
    Iterator i = s.iterator();
    
    while(i.hasNext()) {
      Map.Entry me = (Map.Entry)i.next();
      System.out.println("Key:" + me.getKey() + " Value: " + me.getValue());
    }

  }

}
Key:100 Value: Akash
Key:200 Value: Bharat
Key:300 Value: Chetan
Key:400 Value: Dev
Key:500 Value: Eshwar

Example: setValue() method

We can replace the values in the entrySet using the setValue() method of the Map.Entry interface in Java. We can understand this with an example below.

Here, we are directly creating a Map.Entry object from the EntrySet and iterating using the for loop. Within the for loop, we can set the value for each entry using the setValue() method.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapEntryReplace {

  public static void main(String[] args) {
    HashMap<String,Integer> hm = new HashMap<String, Integer>();
    hm.put("A1020", 1000);
    hm.put("A1021", 4000);
    hm.put("A1022", 3200);
    hm.put("A1023", 2500);
    hm.put("A1024", 4900);
    
    Set<Map.Entry<String, Integer>> s = hm.entrySet();
    
    for(Map.Entry<String, Integer> me : s) {
      System.out.println("Original entry: " + me.getKey() + " : " + me.getValue());
      me.setValue(me.getValue() + 200);
      System.out.println("Revised entry: " + me.getKey() + " : " + me.getValue());
    }

  }

}
Original entry: A1020 : 1000
Revised entry: A1020 : 1200
Original entry: A1021 : 4000
Revised entry: A1021 : 4200
Original entry: A1024 : 4900
Revised entry: A1024 : 5100
Original entry: A1022 : 3200
Revised entry: A1022 : 3400
Original entry: A1023 : 2500
Revised entry: A1023 : 2700

Conclusion

In this tutorial, we have learnt how to use the Map.Entry interface in Java to retrieve the keys and values in every entrySet of a Map.

Reference

Translate ยป