WeakHashMap in Java implements the Map interface and represents a hash table that has weak keys. If a key is not in an ordinary use, the entry from the map will be automatically removed. This is what differentiates it from other Map implementations.
Null and non-null values are supported and the performance is similar to HashMap class in and in terms of initial capacity and load factor, it has the same efficiency.
By default, this class is not synchronised.
Main differences between HashMap and WeakHashMap:
- HashMap has a strong Reference whereas WeakHashMap has a weak Reference.
- HashMap is dominant over Garbage Collector.
- There is no clone() method in WeakHashMap class as it only implements the Map interface.
Constructors in WeakHashMap
- WeakHashMap(): Creates an empty WeakHashMap with a default initial capacity of 16 and load factor of 0.75.
- WeakHashMap(int initialCapacity: Creates an empty WeakHashMap with the specified capacity.
- WeakHashMap(int initialCapacity, float loadFactor): Creates an empty WeakHashMap with the specified capacity and load factor.
- WeakHashMap(Map<? extends K, ? extends V> m): Creates a new WeakHashMap with the same mappings as the specified map.
Methods in WeakHashMap
- void clear(): removes all of the mappings from the current map.
- boolean containsKey(Object key): returns true if the current map contains a mapping for the specified key.
- boolean containsValue(Object value): returns true if there are key/keys in the current map that are mapped to the specified value.
- V get(Object key): returns the value to which the specified key is mapped or null if the map contains no mappings for the specified key.
- boolean isEmpty(): returns the true if the map is empty, false otherwise.
- V put(K key, V value): “puts” the specified value into the specified key in the current map.
- V remove(Object key): removes the mapping for a key from this WeakHashMap if tis present.
- int size(): returns the number of mappings in the map.
For documentation for all the methods, you can visit the Oracle official documentation page.
An example program that performs some of the methods mentioned above:
// importing the necessary library which is under java.util.* import java.util.*; public class WeakHashMapExample { public static void main(String args[])throws Exception { // declaration of an instance of WeakHashMap that takes a number as a key and string as a value Map<Number, String> animals = new WeakHashMap<Number, String>(); // populating the map animals.put(1, "Elephant"); animals.put(2, "Tiger"); animals.put(3, "Lion"); // condition that checks for a certain value if(animals.containsValue("Tiger")) System.out.println("Tiger exists."); // condition that checks for a certain key if(animals.containsKey(3)) System.out.println("'3' key exists."); // removing a specific key animals.remove(1); System.out.println(animals); // deletes all mappings animals.clear(); // check if weakhashmap is empty if(animals.isEmpty()) System.out.println(animals); } }
Output:
Tiger exists. '3' key exists. {3=Lion, 2=Tiger} {}