Class SoftHashMap<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Map<K,​V>

    class SoftHashMap<K,​V>
    extends java.util.AbstractMap<K,​V>
    implements java.io.Serializable
    The original implementation is taken from The Java Specialists' Newsletter [Issue 098] with permission of the original author. Tweaked code by Endre Stolsvik was contributed in December 2009.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  SoftHashMap.KeySoftReference<K,​V>  
      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • Nested classes/interfaces inherited from interface java.util.Map

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

      Fields 
      Modifier and Type Field Description
      private java.util.Map<K,​SoftHashMap.KeySoftReference<K,​V>> hash
      The internal HashMap that will hold the SoftReference.
      private java.lang.ref.ReferenceQueue<V> queue
      Reference queue for cleared SoftReference objects.
    • Constructor Summary

      Constructors 
      Constructor Description
      SoftHashMap()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsKey​(java.lang.Object key)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Returns a copy of the key/values in the map at the point of calling.
      private void expungeStaleEntries()  
      V get​(java.lang.Object key)  
      V put​(K key, V value)  
      V remove​(java.lang.Object key)  
      int size()  
      • Methods inherited from class java.util.AbstractMap

        clone, containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • queue

        private final java.lang.ref.ReferenceQueue<V> queue
        Reference queue for cleared SoftReference objects.
    • Constructor Detail

      • SoftHashMap

        SoftHashMap()
    • Method Detail

      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.AbstractMap<K,​V>
      • expungeStaleEntries

        private void expungeStaleEntries()
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.AbstractMap<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.AbstractMap<K,​V>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.AbstractMap<K,​V>
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<K,​V>
        Overrides:
        size in class java.util.AbstractMap<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Overrides:
        containsKey in class java.util.AbstractMap<K,​V>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Returns a copy of the key/values in the map at the point of calling. However, setValue still sets the value in the actual SoftHashMap.
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Specified by:
        entrySet in class java.util.AbstractMap<K,​V>