Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 896

Map.Entry Interface in Java

$
0
0

Map.Entry interface in Java denotes a map entry (key-value pair). 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 this class.

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());
}
}
}

Output


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 Internally Works in Java
  2. TreeMap in Java
  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 -


Viewing all articles
Browse latest Browse all 896

Trending Articles