Class KeyInValueMap<K,​V>

  • Type Parameters:
    K - key type
    V - value type
    All Implemented Interfaces:
    java.lang.Iterable<V>
    Direct Known Subclasses:
    EntryHashMap

    public abstract class KeyInValueMap<K,​V>
    extends java.lang.Object
    implements java.lang.Iterable<V>
    A map for values that has the key inside the value, so only storing the value directly without a wrapping map entry is sufficient. It can also compute absent values which is not available in Java 7 maps.

    Does not support null keys or values. Not thread safe.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int capacity  
      private float loadFactor  
      private int size  
      private int threshold  
      private java.lang.Object[] values  
    • Constructor Summary

      Constructors 
      Constructor Description
      KeyInValueMap​(int initialCapacity, float loadFactor)
      Create a new map.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract V computeValue​(K key)
      This method must be overridden in subclasses so that values can be computed for missing keys.
      private void createTable​(int newCapacity)  
      V get​(K key, boolean computeIfAbsent)
      Get the value for a key and optionally compute a new value if it is not already present in the map.
      private int getIndex​(int hash)  
      protected abstract K getKey​(V value)
      Get the key for a value.
      private V getValue​(int index)  
      private int hashFromValue​(V value)  
      protected int hashKey​(K key)
      Calculate the hash for a key.
      private boolean isKeyFor​(K key, V value)
      This method must be overridden in subclasses so that values can be checked if they match a given key.
      java.util.Iterator<V> iterator()  
      private void rehash()  
      int size()  
      • Methods inherited from class java.lang.Object

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

        forEach, spliterator
    • Field Detail

      • values

        private java.lang.Object[] values
      • size

        private int size
      • capacity

        private int capacity
      • threshold

        private int threshold
      • loadFactor

        private final float loadFactor
    • Constructor Detail

      • KeyInValueMap

        public KeyInValueMap​(int initialCapacity,
                             float loadFactor)
        Create a new map.
        Parameters:
        initialCapacity - initial storage capacity
        loadFactor - load factor at which to increase the internal storage capacity
    • Method Detail

      • get

        public V get​(K key,
                     boolean computeIfAbsent)
        Get the value for a key and optionally compute a new value if it is not already present in the map. Automatic value computation is done with computeValue(Object) which must be implemented by subclasses.
        Parameters:
        key - key
        computeIfAbsent - If a value is not found and this is set to true, then compute and add a new value using computeValue(Object).
        Returns:
        The value for the key. If computeIfAbsent is false and no matching value exists, then null is returned.
      • size

        public int size()
      • iterator

        public java.util.Iterator<V> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<K>
      • createTable

        private void createTable​(int newCapacity)
      • getValue

        private V getValue​(int index)
      • rehash

        private void rehash()
      • getIndex

        private int getIndex​(int hash)
      • isKeyFor

        private boolean isKeyFor​(K key,
                                 V value)
        This method must be overridden in subclasses so that values can be checked if they match a given key. Called by get(Object, boolean) when there are multiple values that share the same key hash.
        Parameters:
        key - key to check
        value - value to check
        Returns:
        true if key is the key for value, false otherwise
      • computeValue

        protected abstract V computeValue​(K key)
        This method must be overridden in subclasses so that values can be computed for missing keys. Called by get(Object, boolean) when the requested key is missing in the map.
        Parameters:
        key - key to calculate value for
        Returns:
        calculated value
      • hashKey

        protected int hashKey​(K key)
        Calculate the hash for a key. May be overridden by subclasses.
        Parameters:
        key - key to calculate hash for
        Returns:
        hash for key
      • hashFromValue

        private int hashFromValue​(V value)
      • getKey

        protected abstract K getKey​(V value)
        Get the key for a value.
        Parameters:
        value - value to get key for
        Returns:
        key for value