Java Operators

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - The Map.Entry Interface



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
1

boolean equals(Object obj)

Returns true if obj is a Map.Entry whose key and value are equal to that of the invoking object.

2

Object getKey( )

Returns the key for this map entry.

3

Object getValue( )

Returns the value for this map entry.

4

int hashCode( )

Returns the hash code for this map entry.

5

Object 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 1

Following 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.22 

Example 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.2 

Example 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
Advertisements