Class WeakReferenceHashMap<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this map
    V - the type of mapped values
    All Implemented Interfaces:
    java.util.Map<K,​V>

    public class WeakReferenceHashMap<K,​V>
    extends WeakReferenceMap<K,​V>
    This is a Map implementation designed specifically for people intending to create a cache. The keys are held strongly, but the values are held weakly, so the values can be garbage-collected. When an entry is garbage-collected, its key is removed from the Map on the next Map activity.

    The entrySet() and values() methods of this class do not work.

    Author:
    Matthew Wakeling
    See Also:
    WeakReference
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakReferenceHashMap()
      Constructs a new, empty WeakReferenceHashMap with the default initial capacity (16) and the default load factor (0.75).
      WeakReferenceHashMap​(int initialCapacity)
      Constructs a new, empty WeakReferenceHashMap with the given initial capacity and the default load factor, which is 0.75.
      WeakReferenceHashMap​(int initialCapacity, float loadFactor)
      Constructs a new, empty WeakReferenceHashMap with the given initial capacity and the given load factor.
      WeakReferenceHashMap​(java.lang.String name)
      Constructs a new, empty WeakReferenceHashMap with the default initial capacity (16) and the default load factor (0.75), and a name.
      WeakReferenceHashMap​(java.util.Map<K,​V> t)
      Constructs a new WeakReferenceHashMap with the same mappings as the specified Map.
    • Constructor Detail

      • WeakReferenceHashMap

        public WeakReferenceHashMap​(int initialCapacity,
                                    float loadFactor)
        Constructs a new, empty WeakReferenceHashMap with the given initial capacity and the given load factor.
        Parameters:
        initialCapacity - The initial capacity of the WeakReferenceHashMap
        loadFactor - The load factor of the WeakReferenceHashMap
        Throws:
        java.lang.IllegalArgumentException - If the initial capacity is negative, or if the load factor is nonpositive.
      • WeakReferenceHashMap

        public WeakReferenceHashMap​(int initialCapacity)
        Constructs a new, empty WeakReferenceHashMap with the given initial capacity and the default load factor, which is 0.75.
        Parameters:
        initialCapacity - The initial capacity of the WeakReferenceHashMap
        Throws:
        java.lang.IllegalArgumentException - If the initial capacity is negative.
      • WeakReferenceHashMap

        public WeakReferenceHashMap()
        Constructs a new, empty WeakReferenceHashMap with the default initial capacity (16) and the default load factor (0.75).
      • WeakReferenceHashMap

        public WeakReferenceHashMap​(java.lang.String name)
        Constructs a new, empty WeakReferenceHashMap with the default initial capacity (16) and the default load factor (0.75), and a name.
        Parameters:
        name - the name of the WeakReferenceHashMap - printed out in log messages
      • WeakReferenceHashMap

        public WeakReferenceHashMap​(java.util.Map<K,​V> t)
        Constructs a new WeakReferenceHashMap with the same mappings as the specified Map. The WeakReferenceHashMap is created with default load factor, which is 0.75 and an initial capacity sufficient to hold the mappings in the specified Map.
        Parameters:
        t - the map whose mappings are to be placed in this map.
        Throws:
        java.lang.NullPointerException - if the specified map is null.