Class ArrayHashMap<K,​V>

  • All Implemented Interfaces:
    Iterable<V>

    public class ArrayHashMap<K,​V>
    extends Object
    implements Iterable<V>
    An Array-based hashtable which maps, similar to Java's HashMap, only performance tests showed it performs better.

    The hashtable is constructed with a given capacity, or 16 as a default. In case there's not enough room for new pairs, the hashtable grows. Capacity is adjusted to a power of 2, and there are 2 * capacity entries for the hash. The pre allocated arrays (for keys, values) are at length of capacity + 1, where index 0 is used as 'Ground' or 'NULL'.

    The arrays are allocated ahead of hash operations, and form an 'empty space' list, to which the <key,value> pair is allocated.

    WARNING: This API is experimental and might change in incompatible ways in the next release.
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayHashMap()
      Constructs a map with default capacity.
      ArrayHashMap​(int capacity)
      Constructs a map with given capacity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected int calcBaseHashIndex​(K key)
      Calculating the baseHash index using the internal internal hashFactor.
      void clear()
      Empties the map.
      boolean containsKey​(K key)
      Returns true iff the key exists in the map.
      boolean containsValue​(Object o)
      Returns true iff the object exists in the map.
      boolean equals​(Object o)  
      protected int find​(K key)
      Returns the index of the given key, or zero if the key wasn't found.
      V get​(K key)
      Returns the object mapped with the given key, or null if the key wasn't found.
      protected void grow()
      Allocates a new map of double the capacity, and fast-insert the old key-value pairs.
      int hashCode()  
      boolean isEmpty()
      Returns true iff the map is empty.
      Iterator<V> iterator()
      Returns an iterator on the mapped objects.
      Iterator<K> keyIterator()
      Returns an iterator on the map keys.
      V put​(K key, V e)
      Inserts the <key,value> pair into the map.
      V remove​(K key)
      Removes a <key,value> pair from the map and returns the mapped value, or null if the none existed.
      int size()
      Returns number of pairs currently in the map.
      Object[] toArray()
      Translates the mapped pairs' values into an array of Objects
      V[] toArray​(V[] a)
      Translates the mapped pairs' values into an array of V
      String toString()  
    • Constructor Detail

      • ArrayHashMap

        public ArrayHashMap()
        Constructs a map with default capacity.
      • ArrayHashMap

        public ArrayHashMap​(int capacity)
        Constructs a map with given capacity. Capacity is adjusted to a native power of 2, with minimum of 16.
        Parameters:
        capacity - minimum capacity for the map.
    • Method Detail

      • calcBaseHashIndex

        protected int calcBaseHashIndex​(K key)
        Calculating the baseHash index using the internal internal hashFactor.
      • clear

        public void clear()
        Empties the map. Generates the "Empty" space list for later allocation.
      • containsKey

        public boolean containsKey​(K key)
        Returns true iff the key exists in the map.
      • containsValue

        public boolean containsValue​(Object o)
        Returns true iff the object exists in the map.
      • find

        protected int find​(K key)
        Returns the index of the given key, or zero if the key wasn't found.
      • get

        public V get​(K key)
        Returns the object mapped with the given key, or null if the key wasn't found.
      • grow

        protected void grow()
        Allocates a new map of double the capacity, and fast-insert the old key-value pairs.
      • isEmpty

        public boolean isEmpty()
        Returns true iff the map is empty.
      • iterator

        public Iterator<V> iterator()
        Returns an iterator on the mapped objects.
        Specified by:
        iterator in interface Iterable<K>
      • keyIterator

        public Iterator<K> keyIterator()
        Returns an iterator on the map keys.
      • put

        public V put​(K key,
                     V e)
        Inserts the <key,value> pair into the map. If the key already exists, this method updates the mapped value to the given one, returning the old mapped value.
        Returns:
        the old mapped value, or null if the key didn't exist.
      • remove

        public V remove​(K key)
        Removes a <key,value> pair from the map and returns the mapped value, or null if the none existed.
        Parameters:
        key - used to find the value to remove
        Returns:
        the removed value or null if none existed.
      • size

        public int size()
        Returns number of pairs currently in the map.
      • toArray

        public Object[] toArray()
        Translates the mapped pairs' values into an array of Objects
        Returns:
        an object array of all the values currently in the map.
      • toArray

        public V[] toArray​(V[] a)
        Translates the mapped pairs' values into an array of V
        Parameters:
        a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, use as much space as it can.
        Returns:
        an array containing the elements of the list
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object