How to use WeakHashMap in Java
Posted By : Ganesh Chandra Tiwari | 31-Oct-2018
WeakHashMap:
WeakHashMap implements the Map interface that stores only weak references to its keys. Storing only weak references will collect a key-value pair when your key is no longer referenced outside of WeakHashMap.
This class is the easiest way to take advantage of the power of weak references. It is useful to implement "record-like" data structures, where the utility of an entry fades when its key is not accessible to any thread.
WeakHashMap works identically to HashMap with one very important exception: if the Java memory manager no longer has a secure reference to the specified object as a key, the card entry will be removed.
Weak reference: If the only references to an object are weak references, the garbage collector can retrieve the memory of the object at any time. It is not necessary to wait until the system runs out of memory. As a general rule, it will be published the next time you run the garbage collector.
Constructors :
1 WeakHashMap()
This constructor creates a new empty WeakHashMap with default initial capacity (16) and default load factor (0.75).
2 WeakHashMap(int initialCapacity)
This constructor creates a new empty WeakHashMap with the indicated initial capacity and the default load factor, which is 0.75.
3 WeakHashMap(int initialCapacity, float loadFactor)
This constructor creates a new empty WeakHashMap with the given initial capacity and load factor.
4 WeakHashMap(Map t)
This constructor builds a new WeakHashMap with the same assignments as the specified map.
Methods:
1 void clear()
Delete all mappings from this map.
2 boolean containsKey(Object key)
If a map contains specified key it returns true.
3 boolean containsValue(Object value)
If a map maps one or more keys to the specified value it returns true.
4 Set entrySet()
It returns a collection view of the mappings contained in a map.
5 Object get(Object key)
It returns the value at which the key specified in the mapped or weak hash map, or null if the map contains no mapping for this key.
6 boolean isEmpty()
If a map contains no key-value mappings it return true.
7 Set keySet()
It returns a set view of the keys contained in a map.
8 Object put(Object key, Object value)
It is used to add a value with an unique key and associates the specified value with the specified key in a map.
9 void putAll(Map m)
It copies all of the mappings from the specified map to a map.
10 Object remove(Object key)
It removes the mapping for this key from this map if present.
11
It returns the number of key-value mappings in a map.
12 Collection values()
It returns a collection view of the values contained in a map.
Thanks, I hope it will help you
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ganesh Chandra Tiwari
Ganesh is a Web Application Developer. He has knowledge of Spring, Struts, Hibernate, Angular JS, HTML, CSS, Javascript.