The Map.Entry interface enables you to work with a map entry.
The entrySet( ) method declared by the Map interface returns a Set containing the map entries. Each of these set elements is a Map.Entry object.
Following table summarizes the methods declared by this interface −
Sr.No. Method & Description 1boolean equals(Object obj)
Returns true if obj is a Map.Entry whose key and value are equal to that of the invoking object.
2Object getKey( )
Returns the key for this map entry.
3Object getValue( )
Returns the value for this map entry.
4int hashCode( )
Returns the hash code for this map entry.
5Object setValue(Object v)
Sets the value for this map entry to v. A ClassCastException is thrown if v is not the correct type for the map. A NullPointerException is thrown if v is null and the map does not permit null keys. An UnsupportedOperationException is thrown if the map cannot be changed.
Example 1Following is an example showing how Map.Entry can be used to get values of a map entry −
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class HashMapDemo { public static void main(String args[]) { // Create a hash map HashMap<String, Double> hm = new HashMap<>(); // Put elements to the map hm.put("Zara", Double.valueOf(3434.34)); hm.put("Mahnaz", Double.valueOf(123.22)); hm.put("Ayan", Double.valueOf(1378.00)); hm.put("Daisy", Double.valueOf(99.22)); hm.put("Qadir", Double.valueOf(-19.08)); // Get a set of the entries Set<Map.Entry<String, Double>> set = hm.entrySet(); // Get an iterator Iterator<Map.Entry<String, Double>> i = set.iterator(); // Display elements while(i.hasNext()) { Map.Entry<String, Double> me = i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } } }Output
Daisy: 99.22 Ayan: 1378.0 Zara: 3434.34 Qadir: -19.08 Mahnaz: 123.22Example 2
Following is an example showing how Map.Entry can be used to set values of a map entry −
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class HashMapDemo { public static void main(String args[]) { // Create a hash map HashMap<String, Double> hm = new HashMap<>(); // Put elements to the map hm.put("Zara", Double.valueOf(3434.34)); hm.put("Mahnaz", Double.valueOf(123.22)); hm.put("Ayan", Double.valueOf(1378.00)); hm.put("Daisy", Double.valueOf(99.22)); hm.put("Qadir", Double.valueOf(-19.08)); // Get a set of the entries Set<Map.Entry<String, Double>> set = hm.entrySet(); // Get an iterator Iterator<Map.Entry<String, Double>> i = set.iterator(); // Display elements while(i.hasNext()) { Map.Entry<String, Double> me = i.next(); me.setValue(me.getValue() * 10); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } } }Output
Daisy: 992.2 Ayan: 13780.0 Zara: 34343.4 Qadir: -190.79999999999998 Mahnaz: 1232.2Example 3
Following is an example showing how Map.Entry can be used to get key of a map entry −
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class HashMapDemo { public static void main(String args[]) { // Create a hash map HashMap<String, Double> hm = new HashMap<>(); // Put elements to the map hm.put("Zara", Double.valueOf(3434.34)); hm.put("Mahnaz", Double.valueOf(123.22)); hm.put("Ayan", Double.valueOf(1378.00)); hm.put("Daisy", Double.valueOf(99.22)); hm.put("Qadir", Double.valueOf(-19.08)); // Get a set of the entries Set<Map.Entry<String, Double>> set = hm.entrySet(); // Get an iterator Iterator<Map.Entry<String, Double>> i = set.iterator(); // Display elements while(i.hasNext()) { Map.Entry<String, Double> me = i.next(); System.out.println(me.getKey()); } } }Output
Daisy Ayan Zara Qadir Mahnaz
java_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4