Java : Map.Entry with Examples

Map.Entry (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the Map.Entry<K,V> methods.


Summary

A map entry (key-value pair). The Entry may be unmodifiable, or the value may be modifiable if the optional setValue method is implemented. The Entry may be independent of any map, or it may represent an entry of the entry-set view of a map.

Class diagram

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

Returns a comparator that compares Map.Entry in natural order on key.

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)

Returns a comparator that compares Map.Entry by key using the given Comparator.

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 extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue ()

Returns a comparator that compares Map.Entry in natural order on value.

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)

Returns a comparator that compares Map.Entry by value using the given Comparator.

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> Map.Entry<K,V> copyOf (Map.Entry<? extends K,? extends V> e)

Returns a copy of the given Map.Entry.

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)

Compares the specified object with this entry for equality.

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

Returns the key corresponding to this entry.

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

Returns the value corresponding to this entry.

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

int hashCode ()

Returns the hash code value for this map entry.

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)

Replaces the value corresponding to this entry with the specified value (optional operation).

final var entry = Map.entry("a", 10);
System.out.println(entry); // a=10

try {
    var _ = 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

To top of page