Package gnu.trove

Class THashMap<K,V>

All Implemented Interfaces:
TObjectHashingStrategy<K>, Externalizable, Serializable, Cloneable, Map<K,V>

public class THashMap<K,V> extends TObjectHash<K> implements Map<K,V>, Externalizable
An implementation of the Map interface which uses an open addressed hash table to store its contents. Created: Sun Nov 4 08:52:45 2001
Version:
$Id: THashMap.java,v 1.33 2008/05/08 17:42:55 robeden Exp $
Author:
Eric D. Friedman
See Also:
  • Field Details

    • _values

      protected transient V[] _values
      the values of the map
  • Constructor Details

    • THashMap

      public THashMap()
      Creates a new THashMap instance with the default capacity and load factor.
    • THashMap

      public THashMap(TObjectHashingStrategy<K> strategy)
      Creates a new THashMap instance with the default capacity and load factor.
      Parameters:
      strategy - used to compute hash codes and to compare objects.
    • THashMap

      public THashMap(int initialCapacity)
      Creates a new THashMap instance with a prime capacity equal to or greater than initialCapacity and with the default load factor.
      Parameters:
      initialCapacity - an int value
    • THashMap

      public THashMap(int initialCapacity, TObjectHashingStrategy<K> strategy)
      Creates a new THashMap instance with a prime capacity equal to or greater than initialCapacity and with the default load factor.
      Parameters:
      initialCapacity - an int value
      strategy - used to compute hash codes and to compare objects.
    • THashMap

      public THashMap(int initialCapacity, float loadFactor)
      Creates a new THashMap instance with a prime capacity equal to or greater than initialCapacity and with the specified load factor.
      Parameters:
      initialCapacity - an int value
      loadFactor - a float value
    • THashMap

      public THashMap(int initialCapacity, float loadFactor, TObjectHashingStrategy<K> strategy)
      Creates a new THashMap instance with a prime capacity equal to or greater than initialCapacity and with the specified load factor.
      Parameters:
      initialCapacity - an int value
      loadFactor - a float value
      strategy - used to compute hash codes and to compare objects.
    • THashMap

      public THashMap(Map<K,V> map)
      Creates a new THashMap instance which contains the key/value pairs in map.
      Parameters:
      map - a Map value
    • THashMap

      public THashMap(Map<K,V> map, TObjectHashingStrategy<K> strategy)
      Creates a new THashMap instance which contains the key/value pairs in map.
      Parameters:
      map - a Map value
      strategy - used to compute hash codes and to compare objects.
  • Method Details

    • clone

      public THashMap<K,V> clone()
      Overrides:
      clone in class TObjectHash<K>
      Returns:
      a shallow clone of this collection
    • setUp

      protected int setUp(int initialCapacity)
      initialize the value array of the map.
      Overrides:
      setUp in class TObjectHash<K>
      Parameters:
      initialCapacity - an int value
      Returns:
      an int value
    • put

      public V put(K key, V value)
      Inserts a key/value pair into the map.
      Specified by:
      put in interface Map<K,V>
      Parameters:
      key - an Object value
      value - an Object value
      Returns:
      the previous value associated with key, or null if none was found.
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Inserts a key/value pair into the map if the specified key is not already associated with a value.
      Specified by:
      putIfAbsent in interface Map<K,V>
      Parameters:
      key - an Object value
      value - an Object value
      Returns:
      the previous value associated with key, or null if none was found.
    • equals

      public boolean equals(Object other)
      Compares this map with another map for equality of their stored entries.
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
      Parameters:
      other - an Object value
      Returns:
      a boolean value
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • forEachKey

      public boolean forEachKey(TObjectProcedure<K> procedure)
      Executes procedure for each key in the map.
      Parameters:
      procedure - a TObjectProcedure value
      Returns:
      false if the loop over the keys terminated because the procedure returned false for some key.
    • forEachValue

      public boolean forEachValue(TObjectProcedure<V> procedure)
      Executes procedure for each value in the map.
      Parameters:
      procedure - a TObjectProcedure value
      Returns:
      false if the loop over the values terminated because the procedure returned false for some value.
    • forEachEntry

      public boolean forEachEntry(TObjectObjectProcedure<K,V> procedure)
      Executes procedure for each key/value entry in the map.
      Parameters:
      procedure - a TObjectObjectProcedure value
      Returns:
      false if the loop over the entries terminated because the procedure returned false for some entry.
    • retainEntries

      public boolean retainEntries(TObjectObjectProcedure<K,V> procedure)
      Retains only those entries in the map for which the procedure returns a true value.
      Parameters:
      procedure - determines which entries to keep
      Returns:
      true if the map was modified.
    • transformValues

      public void transformValues(TObjectFunction<V,V> function)
      Transform the values in this map using function.
      Parameters:
      function - a TObjectFunction value
    • rehash

      protected void rehash(int newCapacity)
      rehashes the map to the new capacity.
      Specified by:
      rehash in class THash
      Parameters:
      newCapacity - an int value
    • get

      public V get(Object key)
      retrieves the value for key
      Specified by:
      get in interface Map<K,V>
      Parameters:
      key - an Object value
      Returns:
      the value of key or null if no such mapping exists.
    • clear

      public void clear()
      Empties the map.
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class THash
    • remove

      public V remove(Object key)
      Deletes a key/value pair from the map.
      Specified by:
      remove in interface Map<K,V>
      Parameters:
      key - an Object value
      Returns:
      an Object value
    • removeAt

      protected void removeAt(int index)
      removes the mapping at index from the map.
      Overrides:
      removeAt in class TObjectHash<K>
      Parameters:
      index - an int value
    • values

      public Collection<V> values()
      Returns a view on the values of the map.
      Specified by:
      values in interface Map<K,V>
      Returns:
      a Collection value
    • keySet

      public Set<K> keySet()
      returns a Set view on the keys of the map.
      Specified by:
      keySet in interface Map<K,V>
      Returns:
      a Set value
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Returns a Set view on the entries of the map.
      Specified by:
      entrySet in interface Map<K,V>
      Returns:
      a Set value
    • containsValue

      public boolean containsValue(Object val)
      checks for the presence of val in the values of the map.
      Specified by:
      containsValue in interface Map<K,V>
      Parameters:
      val - an Object value
      Returns:
      a boolean value
    • containsKey

      public boolean containsKey(Object key)
      checks for the present of key in the keys of the map.
      Specified by:
      containsKey in interface Map<K,V>
      Parameters:
      key - an Object value
      Returns:
      a boolean value
    • putAll

      public void putAll(Map<? extends K,? extends V> map)
      copies the key/value mappings in map into this map.
      Specified by:
      putAll in interface Map<K,V>
      Parameters:
      map - a Map value
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Overrides:
      writeExternal in class TObjectHash<K>
      Throws:
      IOException
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Specified by:
      readExternal in interface Externalizable
      Overrides:
      readExternal in class TObjectHash<K>
      Throws:
      IOException
      ClassNotFoundException