Java.util.WeakHashMap Class



Introduction

The java.util.WeakHashMap class is a hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed by the garbage collector, when its key is no longer in use. Following are the important points about WeakHashMap −

  • Both null values and the null key are supported.

  • Like most collection classes, this class is also not synchronized.

  • This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator.

  • Each key object in a WeakHashMap is stored indirectly as the referent of a weak reference.

  • This class is a member of the Java Collections Framework.

Class declaration

Following is the declaration for java.util.WeakHashMap class −

public class WeakHashMap<K,V> 
   extends AbstractMap<K,V>
   implements Map<K,V>

Here <K> is the type of keys maintained by this map and <V> is the type of mapped values.

Class constructors

Sr.No. Constructor & Description
1

WeakHashMap()

This constructor is used to create an empty WeakHashMap with the default initial capacity (16) and load factor (0.75).

2

WeakHashMap(int initialCapacity)

This constructor is used to create an empty WeakHashMap with the given initial capacity and the default load factor (0.75).

3

WeakHashMap(int initialCapacity, float loadFactor)

This constructor is used to create an empty WeakHashMap with the given initial capacity and the given load factor.

4

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

This constructor is used to create a new WeakHashMap with the same mappings as the specified map.

Class methods

Sr.No. Method & Description
1 void clear()

This method removes all of the mappings from this map.

2 boolean containsKey(Object key)

This method returns true if this map contains a mapping for the specified key.

3 boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the specified value.

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

This method returns a Set view of the mappings contained in this map.

5 v get(Object key)

This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

6 boolean isEmpty()

This method returns true if this map contains no key-value mappings.

7 Set<K> keySet()

This method returns a Set view of the keys contained in this map.

8 v put(K key, V value)

This method associates the specified value with the specified key in this map.

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

This method copies all of the mappings from the specified map to this map.

10 v remove(Object key)

This method removes the mapping for a key from this weak hash map if it is present.

11 int size()

This method returns the number of key-value mappings in this map.

12 Collection<V> values()

This method returns a Collection view of the values contained in this map.

Methods inherited

This class inherits methods from the following classes −

  • java.util.AbstractMap
  • java.lang.Object
  • java.util.Map
Advertisements