Java : AbstractMap with Examples

AbstractMap (Java SE 18 & JDK 18) API Examples.
You will find code examples on most AbstractMap methods.


Summary

This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.

Class diagram

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

Sole constructor.

For invocation by subclass constructors, typically implicit. Therefore, the code example is omitted.

Methods

void clear ()

Removes all of the mappings from this map (optional operation).

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

Returns a shallow copy of this AbstractMap instance: the keys and values themselves are not cloned.

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)

Returns true if this map contains a mapping for the specified 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)

Returns true if this map maps one or more keys to the specified 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)

Compares the specified object with this map for equality.

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)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the 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 ()

Returns the hash code value for this map.

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

Returns true if this map contains no key-value mappings.

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

Returns a Set view of the keys contained in this map.

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)

Associates the specified value with the specified key in this map (optional operation).

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)

Copies all of the mappings from the specified map to this map (optional operation).

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)

Removes the mapping for a key from this map if it is present (optional operation).

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

Returns the number of key-value mappings in this map.

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

Returns a string representation of this map.

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

Returns a Collection view of the values contained in this map.

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

To top of page