WeakHashMap in Java
In the previous blog we learned about LinkedHashMap and its methods. If you want to learn more about it, visit LinkedHashMap in Java. In this blog, we will go through the WeakHashMap class.
WeakHashMap
Before understanding what WeakHashMap is, we must know what is HashMap. WeakHashMap is an implementation of the Map interface which stores weak references to its keys. Storing just weak references means that a key-value pair can be garbage-collected when its key is no longer referenced outside of the WeakHashMap. WeakHashMap provides a simple way to harness the power of weak references. It is useful for implementing "registry-like" data structures, where the utility of an entry vanishes or disappears when its key is no longer reachable by any thread. WeakHashMap works identically to the HashMap with one difference i.e. If the Java memory manager no longer has a strong reference to the object supplied as a key, the entry in the map will be wiped or destroyed.
WeakHashMap Constructors
- WeakHashMap() - WeakHashMap() constructor constructs the empty WeakHashMap with the default initial capacity and load factor.
- WeakHashMap(int initialCapacity) - WeakHashMap(int initialCapacity) constructs the empty WeakHashMap with the specified initial capacity and default load factor.
- WeakHashMap(int initialCapacity, float loadFactor) - WeakHashMap(int initialCapacity, float loadFactor) creates a new WeakHashMap with the given initial capacity and load factor.
- WeakHashMap(Map map) - WeakHashMap(Map map) generates a new WeakHashMap that contains the same mappings as the specified Map.
Methods of WeakHashMap
- put()
- putAll()
- putIfAbsent()
- entrySet()
- keySet()
- values()
- get()
- get()
- getOrDefult()
- remove()
- clear()
- isEmpty()
- size()
- containsKey()
- containsValue()
put() method adds or inserts the specified key/value mapping to the map.
putAll() method adds or inserts all the elements from the specified map to this map.
The putIfAbsent() method only inserts the specified value with the specified key into the map if it is not already there.
Example of WeakHashMap using the above three methods
Output
entrySet() method returns a set of all the key-value mapping of the map.
keySet() returns a set view of the keys in this map.
values() method returns the view of all the values present in the entries of the map.
Example of WeakHashMap using the above three methods
Output
get() method returns the value of the specified key in the map.
get() method returns the value of the specified key in the map.
The getOrDefult() method returns the value associated with the specified key. If the key is not found, the default value is returned.
Example for LinkedHashMap using the above two methods
Output
remove() method removes or deletes the specified values with the associated specified keys from the map.
clear() method removes all the key-value pairs of the map.
isEmpty() method checks whether the weakhashmap is empty or not.
size() method returns the number of entries in the weakhashmap.
containsKey() method checks if the mapping for the specified key is present in the weakhashmap. If the key is not present then it returns false.
containsValue() method checks if the specified value is present in one or more mappings of the weakhashmap or not. If the value is not present then it returns false.
Example of WeakHashMap using the above methods
Output