Class TimedHashMap<K,​V>

  • Type Parameters:
    K - The key type
    V - The Object type
    All Implemented Interfaces:
    java.util.Map<K,​V>

    public class TimedHashMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    Simple utility class that extends HashMap by adding an expiry to the entries. This map stores entries, and returns them only if the entries were last accessed within a specified timeout period. Otherwise, null is returned. This map does not allow null keys but does allow null values.
    • 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
      TimedHashMap()  
      TimedHashMap​(long timeout, java.util.concurrent.TimeUnit unit)
      Create a new map with a non-default entry timeout period
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      V get​(java.lang.Object key)
      Return cached item if it has not already expired.
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      V put​(K key, V value)
      Store the item in the map and associate a timestamp with it.
      void putAll​(java.util.Map<? extends K,​? extends V> m)  
      V remove​(java.lang.Object key)  
      void setTimeout​(long timeout, java.util.concurrent.TimeUnit unit)
      Specify how long (in nanoseconds) entries are valid for
      int size()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • TimedHashMap

        public TimedHashMap()
      • TimedHashMap

        public TimedHashMap​(long timeout,
                            java.util.concurrent.TimeUnit unit)
        Create a new map with a non-default entry timeout period
        Parameters:
        unit - the units of the timeout
        timeout - the length of the timeout
    • Method Detail

      • setTimeout

        public void setTimeout​(long timeout,
                               java.util.concurrent.TimeUnit unit)
        Specify how long (in nanoseconds) entries are valid for
        Parameters:
        unit - the units of the timeout
        timeout - the length of the timeout
      • put

        public V put​(K key,
                     V value)
        Store the item in the map and associate a timestamp with it. null is not accepted as a key.
        Specified by:
        put in interface java.util.Map<K,​V>
        Parameters:
        key - The key
        value - The value to store
      • get

        public V get​(java.lang.Object key)
        Return cached item if it has not already expired. Before returning, this method also resets the "last accessed" time for this entry, so it is good for another 10 seconds
        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key - The key
      • containsKey

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

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

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • remove

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

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • clear

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

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • values

        public java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>