Class Hashlet<K,​V>


  • public final class Hashlet<K,​V>
    extends java.lang.Object
    Lightweight hash map from key to value with limited functionality. This class lets you build a map from key to value. The value for a key may be overwritten and the put and get methods have the same semantics as for normal Java Maps, but there is no remove operation. Also, there is no iterator support, but keys and values can be accessed directly by index. The access order of keys and values are defined by the insert order of the keys. The goal of this class is to reduce the amount of object that are allocated by packing everything into two internal arrays. The keys and values are packed in an Object array and the hash table and entries are packed in an int array. The internal arrays are not created until space is needed. The default initial capacity is 16 entries. If you know you need much more space than this, you can explicitly reserve more space before starting to insert values. The maximum load factor is 0.7 and drops slightly with increasing capacity.
    Author:
    Havard Pettersen
    • Constructor Summary

      Constructors 
      Constructor Description
      Hashlet()
      Create an empty Hashlet.
      Hashlet​(Hashlet<K,​V> hashlet)
      Create a Hashlet that is a shallow copy of another Hashlet.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)  
      V get​(java.lang.Object key)
      Obtain the value for a specific key.
      int getIndexOfKey​(java.lang.Object key)
      Finds the index where the key,value pair is stored.
      int hashCode()  
      K key​(int i)
      Obtain a key.
      V put​(K key, V value)
      Associate a value with a specific key.
      void reserve​(int n)
      Reserve space for more key value pairs.
      V setValue​(int i, V value)
      This will replace the value at the index give.
      int size()
      The current size.
      V value​(int i)
      Obtain a value.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Hashlet

        public Hashlet()
        Create an empty Hashlet.
      • Hashlet

        public Hashlet​(Hashlet<K,​V> hashlet)
        Create a Hashlet that is a shallow copy of another Hashlet.
        Parameters:
        hashlet - the Hashlet to copy.
    • Method Detail

      • reserve

        public void reserve​(int n)
        Reserve space for more key value pairs. This method is used by the put method to perform rehashing when needed. It can be invoked directly by the application to reduce the number of rehashes needed to insert a large number of entries.
        Parameters:
        n - the number of additional entries to reserve space for
      • size

        public int size()
        The current size. This is the number of key value pairs currently stored in this object.
        Returns:
        current size
      • key

        public K key​(int i)
        Obtain a key. Keys are accessed in the order they were first inserted.
        Parameters:
        i - the index of the key, must be in the range [0, size() - 1]
        Returns:
        the requested key
      • value

        public V value​(int i)
        Obtain a value. Values are accessed in the order in which theirs keys were first inserted.
        Parameters:
        i - the index of the value, must be in the range [0, size() - 1]
        Returns:
        the requested value
      • setValue

        public V setValue​(int i,
                          V value)
        This will replace the value at the index give.
        Parameters:
        i - the index of the value, must be in the range [0, size() - 1]
        value - The new value you want to set for this index.
        Returns:
        previous value
      • put

        public V put​(K key,
                     V value)
        Associate a value with a specific key.
        Parameters:
        key - the key
        value - the value
        Returns:
        the old value for the key, if it was already present
      • get

        public V get​(java.lang.Object key)
        Obtain the value for a specific key.
        Parameters:
        key - the key
        Returns:
        the value for a key, or null if not found
      • getIndexOfKey

        public int getIndexOfKey​(java.lang.Object key)
        Finds the index where the key,value pair is stored.
        Parameters:
        key - to look for
        Returns:
        the index where the key is found or -1 if it is not found
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object