Java Program to Implement WeakHashMap API

This Java program is to Implement WeakHashMap API.Hash table based implementation of the Map interface, with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently from other Map implementations.Both null values and the null key are supported.

Here is the source code of the Java program to Implement WeakHashMap API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.WeakHashMap;
 
public class WeakHashMapImpl<K, V>
{
    private WeakHashMap<K, V> weakHashMap;
 
    /**
     * Constructs a new, empty WeakHashMap with the default initial capacity
     * (16) and load factor (0.75).
     **/
    public WeakHashMapImpl()
    {
        weakHashMap = new WeakHashMap<K, V>();
    }
 
    /**
     * Constructs a new, empty WeakHashMap with the given initial capacity and
     * the default load factor (0.75).
     **/
    public WeakHashMapImpl(int initialCapacity)
    {
        weakHashMap = new WeakHashMap<K, V>(initialCapacity);
    }
 
    /**
     * Constructs a new, empty WeakHashMap with the given initial capacity and
     * the given load factor.
     **/
    public WeakHashMapImpl(int initialCapacity, float loadFactor)
    {
        weakHashMap = new WeakHashMap<K, V>(initialCapacity, loadFactor);
    }
 
    /** Constructs a new WeakHashMap with the same mappings as the specified map. **/
    public WeakHashMapImpl(Map<? extends K, ? extends V> m)
    {
        weakHashMap = new WeakHashMap<K, V>(m);
    }
 
    /** Removes all of the mappings from this map. **/
    public void clear()
    {
        weakHashMap.clear();
    }
 
    /** Returns true if this map contains a mapping for the specified key. **/
    public boolean containsKey(Object key)
    {
        return weakHashMap.containsKey(key);
    }
 
    /** Returns true if this map maps one or more keys to the specified value. **/
    public boolean containsValue(Object value)
    {
        return weakHashMap.containsValue(value);
    }
 
    /** Returns a Set view of the mappings contained in this map. **/
    public Set<Map.Entry<K, V>> entrySet()
    {
        return weakHashMap.entrySet();
    }
 
    /** Returns a Set view of the keys contained in this map. **/
    public Set<K> keySet()
    {
        return weakHashMap.keySet();
    }
 
    /**
     * Returns the value to which the specified key is mapped, or null if this
     * map contains no mapping for the key.
     **/
    public V get(Object key)
    {
        return weakHashMap.get(key);
    }
 
    /** Associates the specified value with the specified key in this map. **/
    public V put(K key, V value)
    {
        return weakHashMap.put(key, value);
    }
 
    /** Copies all of the mappings from the specified map to this map. **/
    public void putAll(Map<? extends K, ? extends V> map)
    {
        weakHashMap.putAll(map);
    }
 
    /** Removes the mapping for this key from this TreeMap if present. **/
    public V remove(Object key)
    {
        return weakHashMap.remove(key);
    }
 
    /** Returns the number of key-value mappings in this map. **/
    public int size()
    {
        return weakHashMap.size();
    }
 
    /** Returns a Collection view of the values contained in this map. **/
    public Collection<V> values()
    {
        return weakHashMap.values();
    }
 
    /** Returns true if this map contains no key-value mappings. **/
    public boolean isEmpty()
    {
        return weakHashMap.isEmpty();
    }
 
    public static void main(String... arg)
    {
 
        WeakHashMapImpl<Integer, Integer> weakhashMap = new WeakHashMapImpl<Integer, Integer>();
        weakhashMap.put(1, 100);
        weakhashMap.put(2, 200);
        weakhashMap.put(3, 300);
 
        Map<Integer, Integer> anotherMap = new HashMap<Integer, Integer>();
        anotherMap.put(4, 400);
        anotherMap.put(5, 500);
 
        weakhashMap.putAll(anotherMap);
        System.out.println("the key set of the weakhashmap is ");
        Set<Integer> keySet = weakhashMap.keySet();
        Iterator<Integer> itr = keySet.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
        System.out.println("the values of the weakhashmap is ");
        Collection<Integer> collectionValues = weakhashMap.values();
        itr = collectionValues.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
        System.out.println("the entry set of the weakhashmap is ");
        Iterator<Entry<Integer, Integer>> eitr;
        Set<Entry<Integer, Integer>> entrySet = weakhashMap.entrySet();
        eitr = entrySet.iterator();
        while (eitr.hasNext())
        {
            System.out.println(eitr.next() + "\t");
        }
        System.out.println("the weakhashmap contains Key 3 :" + weakhashMap.containsKey(3));
        System.out.println("the weakhashmap contains Value 600 :" + weakhashMap.containsValue(600));
        System.out.println("the size of the weakhashmap is " + weakhashMap.size());
        weakhashMap.clear();
        if (weakhashMap.isEmpty())
            System.out.println("the weakhashmap is empty");
        else
            System.out.println("the weakhashmap is not empty");
    }
}
$ javac  WeakHashMapImpl.java
$ java WeakHashMapImpl
the key set of the weakhashmap is 
5	4	3	2	1	
the values of the weakhashmap is 
500	400	300	200	100	
the entry set of the weakhashmap is 
5=500	
4=400	
3=300	
2=200	
1=100	
the weakhashmap contains Key 3 :true
the weakhashmap contains Value 600 :false
the size of the weakhashmap is 5
the weakhashmap is empty

Related posts:

Lập trình hướng đối tượng (OOPs) trong java
Creating Docker Images with Spring Boot
Weak References in Java
Introduction to Eclipse Collections
Java Program for Topological Sorting in Graphs
Java Program to Implement LinkedBlockingQueue API
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
File Upload with Spring MVC
Concrete Class in Java
Java Program to Permute All Letters of an Input String
Java Program to Find a Good Feedback Vertex Set
Java Program to Check if it is a Sparse Matrix
Java InputStream to String
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Simple Single Sign-On with Spring Security OAuth2
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Date Time trong Java 8
Spring MVC Async vs Spring WebFlux
The Difference Between Collection.stream().forEach() and Collection.forEach()
Consumer trong Java 8
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Using a Mutex Object in Java
New Features in Java 10
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Java Program to Implement Self Balancing Binary Search Tree
Logging a Reactive Sequence
Java Program to Represent Graph Using Adjacency Matrix
Java InputStream to Byte Array and ByteBuffer
Java Program to Implement Quick Sort with Given Complexity Constraint
Spring MVC and the @ModelAttribute Annotation
Convert Time to Milliseconds in Java
Java Program to Create a Random Linear Extension for a DAG