Java : AbstractMap with Examples
AbstractMap (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the AbstractMap<K,V> methods.
Summary
class MyMap extends AbstractMap<String, Integer> {
private final Set<Entry<String, Integer>> entries;
MyMap(String key1, int value1, String key2, int value2, String key3, int value3) {
if (Objects.equals(key1, key2) || Objects.equals(key1, key3) || Objects.equals(key2, key3)) {
throw new IllegalArgumentException("duplicate element");
}
this.entries = Set.of(
Map.entry(key1, value1),
Map.entry(key2, value2),
Map.entry(key3, value3)
);
}
@Override
public Set<Entry<String, Integer>> entrySet() {
return entries;
}
}
final var map = new MyMap("aaa", 10, "bbb", 20, "ccc", 30);
System.out.println(map); // {ccc=30, aaa=10, bbb=20}
System.out.println(map.size()); // 3
System.out.println("-- entries --");
for (final var entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
// Result
// ↓
//-- entries --
//ccc : 30
//aaa : 10
//bbb : 20
Constructors
AbstractMap ()
class MyMap extends AbstractMap<String, Integer> {
private final Set<Entry<String, Integer>> entries;
MyMap(String key1, int value1, String key2, int value2, String key3, int value3) {
if (Objects.equals(key1, key2) || Objects.equals(key1, key3) || Objects.equals(key2, key3)) {
throw new IllegalArgumentException("duplicate element");
}
this.entries = Set.of(
Map.entry(key1, value1),
Map.entry(key2, value2),
Map.entry(key3, value3)
);
}
@Override
public Set<Entry<String, Integer>> entrySet() {
return entries;
}
}
final var map = new MyMap("aaa", 10, "bbb", 20, "ccc", 30);
System.out.println(map); // {ccc=30, aaa=10, bbb=20}
System.out.println(map.size()); // 3
System.out.println("-- entries --");
for (final var entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
// Result
// ↓
//-- entries --
//ccc : 30
//aaa : 10
//bbb : 20
Methods
void clear ()
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3
map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0
protected Object clone ()
class MyMap extends AbstractMap<String, Integer> implements Cloneable {
private final Set<Entry<String, Integer>> entries;
MyMap(String key1, int value1, String key2, int value2, String key3, int value3) {
if (Objects.equals(key1, key2) || Objects.equals(key1, key3)
|| Objects.equals(key2, key3)) {
throw new IllegalArgumentException("duplicate element");
}
this.entries = Set.of(
Map.entry(key1, value1),
Map.entry(key2, value2),
Map.entry(key3, value3)
);
}
@Override
public Set<Entry<String, Integer>> entrySet() {
return entries;
}
@Override
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
}
final var map = new MyMap("aaa", 10, "bbb", 20, "ccc", 30);
System.out.println(map); // {ccc=30, aaa=10, bbb=20}
final var cloned = map.clone();
System.out.println(cloned); // {ccc=30, aaa=10, bbb=20}
boolean containsKey (Object key)
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.containsKey("a")); // true
System.out.println(map.containsKey("b")); // true
System.out.println(map.containsKey("c")); // true
System.out.println(map.containsKey("X")); // false
System.out.println(map.containsKey("")); // false
boolean containsValue (Object value)
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("X", 3);
System.out.println(map); // {a=1, b=2, c=3, X=3}
System.out.println(map.containsValue(0)); // false
System.out.println(map.containsValue(1)); // true
System.out.println(map.containsValue(2)); // true
System.out.println(map.containsValue(3)); // true
System.out.println(map.containsValue(4)); // false
boolean equals (Object o)
final AbstractMap<String, Integer> map1 = new HashMap<>();
map1.put("a", 1);
map1.put("b", 2);
map1.put("c", 3);
System.out.println(map1); // {a=1, b=2, c=3}
final AbstractMap<String, Integer> map2 = new HashMap<>();
map2.put("a", 1);
map2.put("b", 2);
map2.put("c", 3);
System.out.println(map2); // {a=1, b=2, c=3}
final AbstractMap<String, Integer> map3 = new HashMap<>();
map3.put("A", 1);
map3.put("B", 2);
map3.put("C", 3);
System.out.println(map3); // {A=1, B=2, C=3}
final AbstractMap<String, Integer> map4 = new HashMap<>();
map4.put("a", 10);
map4.put("b", 20);
map4.put("c", 30);
System.out.println(map4); // {a=10, b=20, c=30}
System.out.println(map1.equals(map2)); // true
System.out.println(map1.equals(map3)); // false
System.out.println(map1.equals(map4)); // false
V get (Object key)
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.get("a")); // 1
System.out.println(map.get("b")); // 2
System.out.println(map.get("c")); // 3
System.out.println(map.get("X")); // null
System.out.println(map.get("")); // null
int hashCode ()
final AbstractMap<String, Integer> map = new HashMap<>();
System.out.println(map); // {}
System.out.println(map.hashCode()); // 0
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
System.out.println(map); // {a=1, b=2}
System.out.println(map.hashCode()); // 192
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
System.out.println(map); // {A=1, B=2}
System.out.println(map.hashCode()); // 128
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 10);
map.put("b", 20);
System.out.println(map); // {a=10, b=20}
System.out.println(map.hashCode()); // 225
boolean isEmpty ()
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3
System.out.println(map.isEmpty()); // false
map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0
System.out.println(map.isEmpty()); // true
Set<K> keySet ()
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var keys = map.keySet();
System.out.println(keys); // [a, b, c]
map.put("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
System.out.println(keys); // [a, b, c, d]
keys.remove("a");
System.out.println(map); // {b=2, c=3, d=4}
System.out.println(keys); // [b, c, d]
V put (K key, V value)
final AbstractMap<String, Integer> map = new HashMap<>();
System.out.println(map); // {}
System.out.println(map.put("a", 1)); // null
System.out.println(map); // {a=1}
System.out.println(map.put("b", 2)); // null
System.out.println(map); // {a=1, b=2}
System.out.println(map.put("c", 3)); // null
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.put("a", 999)); // 1
System.out.println(map); // {a=999, b=2, c=3}
void putAll (Map<? extends K,? extends V> m)
final AbstractMap<String, Integer> map = new HashMap<>();
System.out.println(map); // {}
map.putAll(Map.of());
System.out.println(map); // {}
map.putAll(Map.of("a", 1, "b", 2));
System.out.println(map); // {a=1, b=2}
map.putAll(Map.of("c", 3, "d", 4, "e", 5));
System.out.println(map); // {a=1, b=2, c=3, d=4, e=5}
map.putAll(Map.of("a", 901, "b", 902, "z", 100));
System.out.println(map); // {a=901, b=902, z=100, c=3, d=4, e=5}
V remove (Object key)
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.remove("b")); // 2
System.out.println(map); // {a=1, c=3}
System.out.println(map.remove("z")); // null
System.out.println(map); // {a=1, c=3}
int size ()
final AbstractMap<String, Integer> map = new HashMap<>();
System.out.println(map); // {}
System.out.println(map.size()); // 0
map.put("a", 1);
System.out.println(map); // {a=1}
System.out.println(map.size()); // 1
map.put("b", 2);
System.out.println(map); // {a=1, b=2}
System.out.println(map.size()); // 2
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3
String toString ()
final AbstractMap<String, Integer> map = new HashMap<>();
final var str1 = map.toString();
System.out.println(str1); // {}
map.put("a", 1);
final var str2 = map.toString();
System.out.println(str2); // {a=1}
map.put("b", 2);
final var str3 = map.toString();
System.out.println(str3); // {a=1, b=2}
map.put("c", 3);
final var str4 = map.toString();
System.out.println(str4); // {a=1, b=2, c=3}
Collection<V> values ()
final AbstractMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var values = map.values();
System.out.println(values); // [1, 2, 3]
map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(values); // [1, 20, 3]
values.remove(1);
System.out.println(map); // {b=20, c=3}
System.out.println(values); // [20, 3]
//values.add(4); // UnsupportedOperationException
Methods declared in Map
compute, computeIfAbsent, computeIfPresent, entrySet, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
Please see the link below.
Related posts
- API Examples