X
    Categories: Java Core

Java WeakHashMap Example

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:

  1. HashMap has a strong Reference whereas WeakHashMap has a weak Reference.
  2. HashMap is dominant over Garbage Collector.
  3. There is no clone() method in WeakHashMap class as it only implements the Map interface.

WeakHashMap implements Map and extends AbstractMap.

Constructors in WeakHashMap

  1. WeakHashMap(): Creates an empty WeakHashMap with a default initial capacity of 16 and load factor of 0.75.
  2. WeakHashMap(int initialCapacity: Creates an empty WeakHashMap with the specified capacity.
  3. WeakHashMap(int initialCapacity, float loadFactor): Creates an empty WeakHashMap with the specified capacity and load factor.
  4. WeakHashMap(Map<? extends K, ? extends V> m): Creates a new WeakHashMap with the same mappings as the specified map.



Methods in WeakHashMap

  1. void clear(): removes all of the mappings from the current map.
  2. boolean containsKey(Object key): returns true if the current map contains a mapping for the specified key.
  3. boolean containsValue(Object value): returns true if there are key/keys in the current map that are mapped to the specified value.
  4. 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.
  5. boolean isEmpty(): returns the true if the map is empty, false otherwise.
  6. V put(K key, V value): “puts” the specified value into the specified key in the current map.
  7. V remove(Object key): removes the mapping for a key from this WeakHashMap if tis present.
  8. 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}
{}

 

0 0 votes
Article Rating
ym_coding:
Related Post