Java : Map.Entry with Examples
Map.Entry (Java SE 20 & JDK 20) with Examples.
You will find code examples on most Map.Entry methods.
Summary
final var entry1 = Map.entry("a", 10);
System.out.println(entry1); // a=10
final var entry2 = Map.entry("b", 20);
System.out.println(entry2); // b=20
final var entry3 = Map.entry("c", 30);
System.out.println(entry3); // c=30
final var map = Map.ofEntries(entry1, entry2, entry3);
System.out.println(map); // {b=20, c=30, a=10}
final var map = new HashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println("-- entries --");
for (final var entry : map.entrySet()) {
System.out.printf("key = %s : value = %d%n",
entry.getKey(), entry.getValue());
}
// Result
// ↓
//-- entries --
//key = a : value = 10
//key = b : value = 20
//key = c : value = 30
Methods
static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey ()
final var map = Map.ofEntries(
Map.entry("b", 10),
Map.entry("a", 20),
Map.entry("d", 30),
Map.entry("c", 40)
);
{
final var comparator = Map.Entry.<String, Integer>comparingByKey();
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [a=20, b=10, c=40, d=30]
}
{
final var comparator = Map.Entry.<String, Integer>comparingByKey(
Comparator.reverseOrder());
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [d=30, c=40, b=10, a=20]
}
static <K, V> Comparator<Map.Entry<K,V>> comparingByKey (Comparator<? super K> cmp)
Please see comparingByKey().
static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue ()
final var map = Map.ofEntries(
Map.entry("a", 20),
Map.entry("b", 10),
Map.entry("c", 40),
Map.entry("d", 30)
);
{
final var comparator = Map.Entry.<String, Integer>comparingByValue();
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [b=10, a=20, d=30, c=40]
}
{
final var comparator = Map.Entry.<String, Integer>comparingByValue(
Comparator.reverseOrder());
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [c=40, d=30, a=20, b=10]
}
static <K, V> Comparator<Map.Entry<K,V>> comparingByValue (Comparator<? super V> cmp)
Please see comparingByValue().
static <K, V> Map.Entry<K,V> copyOf (Map.Entry<? extends K,? extends V> e)
final var entry = new AbstractMap.SimpleEntry<>("a", 10);
System.out.println(entry); // a=10
final var copied = Map.Entry.copyOf(entry);
System.out.println(copied); // a=10
entry.setValue(999);
System.out.println(entry); // a=999
System.out.println(copied); // a=10
boolean equals (Object o)
final var entry1 = Map.entry("a", 10);
final var entry2 = Map.entry("a", 10);
System.out.println(entry1.equals(entry2)); // true
final var entry1 = Map.entry("a", 10);
final var entry2 = Map.entry("a", 20);
final var entry3 = Map.entry("b", 20);
System.out.println(entry1.equals(entry2)); // false
System.out.println(entry1.equals(entry3)); // false
System.out.println(entry2.equals(entry3)); // false
K getKey ()
final var map = new HashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println("-- entries --");
for (final var entry : map.entrySet()) {
System.out.printf("key = %s : value = %d%n",
entry.getKey(), entry.getValue());
}
// Result
// ↓
//-- entries --
//key = a : value = 10
//key = b : value = 20
//key = c : value = 30
V getValue ()
Please see getKey().
int hashCode ()
final var entry1 = Map.entry("a", 10);
System.out.println(entry1.hashCode()); // 107
final var entry2 = Map.entry("a", 20);
System.out.println(entry2.hashCode()); // 117
final var entry3 = Map.entry("b", 20);
System.out.println(entry3.hashCode()); // 118
V setValue (V value)
final var entry = Map.entry("a", 10);
System.out.println(entry); // a=10
try {
final var ret = entry.setValue(999);
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedOperationException! : not supported
final var entry = new AbstractMap.SimpleEntry<>("a", 10);
System.out.println(entry); // a=10
final var ret = entry.setValue(999);
System.out.println(ret); // 10
System.out.println(entry); // a=999
Related posts
- API Examples