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
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)
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)
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)
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 ()
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 ()
Please see getKey().
int hashCode ()
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)
Please see getKey().
String toString ()
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
- API Examples