Java : WeakHashMap with Examples

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


Summary

Hash table based implementation of the Map interface, with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use.

Class diagram

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();

var key1 = new Key("aaa");
var key2 = new Key("bbb");

map.put(key1, 100);
map.put(key2, 200);

System.out.println(map); // {bbb=200, aaa=100}

System.gc();

System.out.println(map); // {bbb=200, aaa=100}

key1 = null;
System.gc();

System.out.println(map); // {bbb=200}

key2 = null;
System.gc();

System.out.println(map); // {}

Constructors

WeakHashMap ()

Constructs a new, empty WeakHashMap with the default initial capacity (16) and load factor (0.75).

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
System.out.println(map); // {}
System.out.println(map.isEmpty()); // true

map.put(new Key("a"), 10);
map.put(new Key("b"), 20);
map.put(new Key("c"), 30);

System.out.println(map); // {a=10, c=30, b=20}
System.out.println(map.size()); // 3

WeakHashMap (int initialCapacity)

Constructs a new, empty WeakHashMap with the given initial capacity and the default load factor (0.75).

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>(10000000);
System.out.println(map); // {}

final var startTime = System.nanoTime();

for (int i = 0; i < 1000000; i++) {
    map.put(new Key(String.valueOf(i)), i);
}

final var endTime = System.nanoTime();

// 0.205931 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var map = new WeakHashMap<Key, Integer>(1);
System.out.println(map); // {}

final var startTime = System.nanoTime();

for (int i = 0; i < 1000000; i++) {
    map.put(new Key(String.valueOf(i)), i);
}

final var endTime = System.nanoTime();

// 0.278058 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);

WeakHashMap (int initialCapacity, float loadFactor)

Constructs a new, empty WeakHashMap with the given initial capacity and the given load factor.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>(16, 0.05f);
System.out.println(map); // {}

final var startTime = System.nanoTime();

for (int i = 0; i < 1000000; i++) {
    map.put(new Key(String.valueOf(i)), i);
}

final var endTime = System.nanoTime();

// 0.367535 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var map = new WeakHashMap<Key, Integer>(16, 0.75f);
System.out.println(map); // {}

final var startTime = System.nanoTime();

for (int i = 0; i < 1000000; i++) {
    map.put(new Key(String.valueOf(i)), i);
}

final var endTime = System.nanoTime();

// 0.285230 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);

WeakHashMap (Map<? extends K,? extends V> m)

Constructs a new WeakHashMap with the same mappings as the specified map.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var m = Map.of(new Key("a"), 10, new Key("b"), 20, new Key("c"), 30);

final var map = new WeakHashMap<>(m);
System.out.println(map); // {a=10, c=30, b=20}
System.out.println(map.size()); // 3

Methods

void clear ()

Removes all of the mappings from this map.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
map.put(new Key("a"), 10);
map.put(new Key("b"), 20);
map.put(new Key("c"), 30);

System.out.println(map); // {a=10, c=30, b=20}
System.out.println(map.size()); // 3

map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0

boolean containsKey (Object key)

Returns true if this map contains a mapping for the specified key.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
map.put(new Key("a"), 10);
map.put(new Key("b"), 20);
map.put(new Key("c"), 30);

System.out.println(map); // {a=10, c=30, b=20}

System.out.println(map.containsKey(new Key("a"))); // true
System.out.println(map.containsKey(new Key("b"))); // true
System.out.println(map.containsKey(new Key("c"))); // true
System.out.println(map.containsKey(new Key("X"))); // false
System.out.println(map.containsKey(new Key(""))); // false

boolean containsValue (Object value)

Returns true if this map maps one or more keys to the specified value.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
map.put(new Key("a"), 10);
map.put(new Key("b"), 20);
map.put(new Key("c"), 30);
map.put(new Key("X"), 30);

System.out.println(map); // {X=30, a=10, c=30, b=20}

System.out.println(map.containsValue(0)); // false
System.out.println(map.containsValue(10)); // true
System.out.println(map.containsValue(20)); // true
System.out.println(map.containsValue(30)); // true
System.out.println(map.containsValue(40)); // false

Set<Map.Entry<K,V>> entrySet ()

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

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
map.put(new Key("a"), 1);
map.put(new Key("b"), 2);
map.put(new Key("c"), 3);

System.out.println(map); // {a=1, c=3, b=2}

final var entries = map.entrySet();
System.out.println(entries); // [a=1, c=3, b=2]

map.replace(new Key("b"), 20);
System.out.println(map); // {a=1, c=3, b=20}
System.out.println(entries); // [a=1, c=3, b=20]

entries.remove(Map.entry(new Key("a"), 1));
System.out.println(map); // {c=3, b=20}
System.out.println(entries); // [c=3, b=20]

for (final var entry : entries) {
    entry.setValue(entry.getValue() * 10);
}

System.out.println(map); // {c=30, b=200}
System.out.println(entries); // [c=30, b=200]

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.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
map.put(new Key("a"), 10);
map.put(new Key("b"), 20);
map.put(new Key("c"), 30);

System.out.println(map); // {a=10, c=30, b=20}

System.out.println(map.get(new Key("a"))); // 10
System.out.println(map.get(new Key("b"))); // 20
System.out.println(map.get(new Key("c"))); // 30
System.out.println(map.get(new Key("X"))); // null
System.out.println(map.get(new Key(""))); // null

boolean isEmpty ()

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

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
map.put(new Key("a"), 10);
map.put(new Key("b"), 20);
map.put(new Key("c"), 30);

System.out.println(map); // {a=10, c=30, b=20}
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.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
map.put(new Key("a"), 10);
map.put(new Key("b"), 20);
map.put(new Key("c"), 30);

System.out.println(map); // {a=10, c=30, b=20}

final var keys = map.keySet();
System.out.println(keys); // [a, c, b]

map.put(new Key("d"), 40);
System.out.println(map); // {a=10, c=30, b=20, d=40}
System.out.println(keys); // [a, c, b, d]

keys.remove(new Key("a"));
System.out.println(map); // {c=30, b=20, d=40}
System.out.println(keys); // [c, b, d]

V put (K key, V value)

Associates the specified value with the specified key in this map.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
System.out.println(map); // {}

System.out.println(map.put(new Key("a"), 1)); // null
System.out.println(map); // {a=1}

System.out.println(map.put(new Key("b"), 2)); // null
System.out.println(map); // {a=1, b=2}

System.out.println(map.put(new Key("c"), 3)); // null
System.out.println(map); // {a=1, c=3, b=2}

System.out.println(map.put(new Key("a"), 999)); // 1
System.out.println(map); // {a=999, c=3, b=2}

void putAll (Map<? extends K,? extends V> m)

Copies all of the mappings from the specified map to this map.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
System.out.println(map); // {}

map.putAll(Map.of());
System.out.println(map); // {}

map.putAll(Map.of(new Key("a"), 1, new Key("b"), 2));
System.out.println(map); // {a=1, b=2}

map.putAll(Map.of(new Key("c"), 3, new Key("d"), 4, new Key("e"), 5));
System.out.println(map); // {a=1, c=3, b=2, e=5, d=4}

map.putAll(Map.of(new Key("a"), 901, new Key("b"), 902, new Key("z"), 100));
System.out.println(map);  // {z=100, a=901, c=3, b=902, e=5, d=4}

V remove (Object key)

Removes the mapping for a key from this weak hash map if it is present.

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
map.put(new Key("a"), 10);
map.put(new Key("b"), 20);
map.put(new Key("c"), 30);

System.out.println(map); // {a=10, c=30, b=20}

System.out.println(map.remove(new Key("b"))); // 20
System.out.println(map); // {a=10, c=30}

System.out.println(map.remove(new Key("z"))); // null
System.out.println(map); // {a=10, c=30}

int size ()

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

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();

System.out.println(map); // {}
System.out.println(map.size()); // 0

map.put(new Key("a"), 10);
System.out.println(map); // {a=10}
System.out.println(map.size()); // 1

map.put(new Key("b"), 20);
System.out.println(map); // {a=10, b=20}
System.out.println(map.size()); // 2

map.put(new Key("c"), 30);
System.out.println(map); // {a=10, c=30, b=20}
System.out.println(map.size()); // 3

Collection<V> values ()

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

record Key(String s) {
    @Override
    public String toString() {
        return s;
    }
}

final var map = new WeakHashMap<Key, Integer>();
map.put(new Key("a"), 1);
map.put(new Key("b"), 2);
map.put(new Key("c"), 3);

System.out.println(map); // {a=1, c=3, b=2}

final var values = map.values();
System.out.println(values); // [1, 3, 2]

map.replace(new Key("b"), 20);
System.out.println(map); // {a=1, c=3, b=20}
System.out.println(values); // [1, 3, 20]

values.remove(1);
System.out.println(map); // {c=3, b=20}
System.out.println(values); // [3, 20]

//values.add(4); // UnsupportedOperationException

Methods declared in AbstractMap

clone, equals, hashCode, toString

Please see the link below.

Methods declared in Map

compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll

Please see the link below.


Related posts

To top of page