Java : AbstractMap.SimpleImmutableEntry with Examples

AbstractMap.SimpleImmutableEntry (Java SE 20 & JDK 20) with Examples.
You will find code examples on most AbstractMap.SimpleImmutableEntry methods.


Summary

An unmodifiable Entry maintaining a key and a value. This class does not support the setValue method. Instances of this class are not associated with any map's entry-set view.

Class diagram

Please see also the Map::entry method.

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

final var entry2 = new AbstractMap.SimpleImmutableEntry<>("b", 20);
System.out.println(entry2); // b=20

final var entry3 = new AbstractMap.SimpleEntry<>("c", 30);
System.out.println(entry3); // c=30

final var map1 = Map.ofEntries(entry1, entry2, entry3);
System.out.println(map1); // {a=10, b=20, c=30}

entry3.setValue(999);
System.out.println(entry3); // c=999

final var map2 = Map.ofEntries(entry1, entry2, entry3);
System.out.println(map2); // {a=10, b=20, c=999}

Constructors

SimpleImmutableEntry (Map.Entry<? extends K,? extends V> entry)

Creates an entry representing the same mapping as the specified entry.

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

final var entry2 = new AbstractMap.SimpleImmutableEntry<>(entry1);
System.out.println(entry2); // a=10

SimpleImmutableEntry (K key, V value)

Creates an entry representing a mapping from the specified key to the specified value.

final var entry1 = new AbstractMap.SimpleImmutableEntry<>("a", 10);
System.out.println(entry1); // a=10

final var entry2 = new AbstractMap.SimpleImmutableEntry<>("b", 20);
System.out.println(entry2); // b=20

Methods

boolean equals (Object o)

Compares the specified object with this entry for equality.

final var entry1 = new AbstractMap.SimpleImmutableEntry<>("a", 10);
final var entry2 = new AbstractMap.SimpleImmutableEntry<>("a", 10);

System.out.println(entry1.equals(entry2)); // true
final var entry1 = new AbstractMap.SimpleImmutableEntry<>("a", 10);
final var entry2 = new AbstractMap.SimpleImmutableEntry<>("a", 20);
final var entry3 = new AbstractMap.SimpleImmutableEntry<>("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 entry = new AbstractMap.SimpleImmutableEntry<>("a", 10);
System.out.println(entry); // a=10
System.out.println(entry.getKey()); // a
System.out.println(entry.getValue()); // 10

try {
    final var ret = entry.setValue(999);
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

V getValue ()

Returns the value corresponding to this entry.

Please see getKey().

int hashCode ()

Returns the hash code value for this map entry.

final var entry1 = new AbstractMap.SimpleImmutableEntry<>("a", 10);
System.out.println(entry1.hashCode()); // 107

final var entry2 = new AbstractMap.SimpleImmutableEntry<>("a", 20);
System.out.println(entry2.hashCode()); // 117

final var entry3 = new AbstractMap.SimpleImmutableEntry<>("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).

Please see getKey().

String toString ()

Returns a String representation of this map entry.

final var entry1 = new AbstractMap.SimpleImmutableEntry<>("a", 10);
final var str1 = entry1.toString();
System.out.println(str1); // a=10

final var entry2 = new AbstractMap.SimpleImmutableEntry<>("b", 20);
final var str2 = entry2.toString();
System.out.println(str2); // b=20

Related posts

To top of page