Class HashMap<T,K,V>

java.lang.Object
org.javimmutable.collections.common.AbstractMap<K,V>
org.javimmutable.collections.hash.HashMap<T,K,V>
All Implemented Interfaces:
Serializable, Iterable<IMapEntry<K,V>>, ArrayAssignMapper<K,V,ArrayMapNode<K,V>>, ArrayDeleteMapper<K,ArrayMapNode<K,V>>, ArrayFindEntryMapper<K,V,ArrayMapNode<K,V>>, ArrayGetMapper<K,V,ArrayMapNode<K,V>>, ArrayIterationMapper<K,V,ArrayMapNode<K,V>>, ArraySizeMapper<ArrayMapNode<K,V>>, ArrayUpdateMapper<K,V,ArrayMapNode<K,V>>, ICollection<IMapEntry<K,V>>, IMap<K,V>, InvariantCheckable, IStreamable<IMapEntry<K,V>>, Mapped<K,V>, SplitableIterable<IMapEntry<K,V>>

@Immutable public class HashMap<T,K,V> extends AbstractMap<K,V> implements ArrayUpdateMapper<K,V,ArrayMapNode<K,V>>, ArrayFindEntryMapper<K,V,ArrayMapNode<K,V>>, ArrayIterationMapper<K,V,ArrayMapNode<K,V>>, ArrayDeleteMapper<K,ArrayMapNode<K,V>>, Serializable
See Also:
  • Method Details

    • of

      public static <K, V> EmptyHashMap<K,V> of()
      Returns an empty hash map. The empty map will automatically select a collision handling strategy on the first call to assign() based on the key for that call. For this reason all keys used for a given map must either implement or not implement Comparable. If some keys implement it and some do not the collision handling code will likely fail due to a class cast exception or a method not defined exception.
    • of

      public static <K, V> IMap<K,V> of(Class<K> klass)
      Returns an empty map using the appropriate collision handling strategy for keys of the given class. All keys used with that map should derive from the specified class to avoid runtime problems with incompatible keys.
    • forKey

      public static <K, V> IMap<K,V> forKey(K key)
      Returns an empty map using the appropriate collision handling strategy for the given key's class. All keys used with that map should derive from the specified key's class to avoid runtime problems with incompatible keys.
    • usingList

      public static <K, V> IMap<K,V> usingList()
      Returns an empty map using linked lists for handling hash code collisions. This is safe for any type of key but is slower when many keys have the same hash code.
    • usingTree

      public static <K extends Comparable<K>, V> IMap<K,V> usingTree()
      Returns an empty map using linked lists for handling hash code collisions. This is faster than the list based collision handling but depends on all keys implementing Comparable and being able to compare themselves to all other keys.
    • builder

      public static <K, V> IMapBuilder<K,V> builder()
    • mapBuilder

      @Nonnull public IMapBuilder<K,V> mapBuilder()
      Description copied from interface: IMap
      Creates a Builder with the same type signature as this Map.
      Specified by:
      mapBuilder in interface IMap<T,K>
    • createMapCollector

      @Nonnull public static <K, V> java.util.stream.Collector<IMapEntry<K,V>,?,IMap<K,V>> createMapCollector()
    • getValueOr

      public V getValueOr(K key, V defaultValue)
      Description copied from interface: Mapped
      Return the value associated with key or defaultValue if no value is associated. Note that if defaultValue is an acceptable value to the container then this method will be ambiguous and find() should be used instead.
      Specified by:
      getValueOr in interface Mapped<T,K>
      Parameters:
      key - identifies the value to retrieve
      defaultValue - value to return if no entry exists for key
      Returns:
      value associated with key or defaultValue if no value is associated
    • find

      @Nonnull public Maybe<V> find(@Nonnull K key)
      Description copied from interface: IMap
      Search for a value within the map and return a Holder indicating if the value was found and, if it was found, the value itself. Holder allows null values to be returned unambiguously.
      Specified by:
      find in interface IMap<T,K>
      Specified by:
      find in interface Mapped<T,K>
      Parameters:
      key - non-null key to search for
      Returns:
      empty Holder if not found, otherwise filled Holder with value
    • findEntry

      @Nonnull public Maybe<IMapEntry<K,V>> findEntry(@Nonnull K key)
      Description copied from interface: IMap
      Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.
      Specified by:
      findEntry in interface IMap<T,K>
      Parameters:
      key - non-null key to search for
      Returns:
      empty Holder if not found, otherwise filled Holder with Entry
    • assign

      @Nonnull public IMap<K,V> assign(@Nonnull K key, V value)
      Description copied from interface: IMap
      Sets the value associated with a specific key. Key must be non-null but value can be null. If the key already has a value in the map the old value is discarded and the new value is stored in its place. Returns a new IMap reflecting any changes. The original map is always left unchanged.
      Specified by:
      assign in interface IMap<T,K>
      Parameters:
      key - non-null key
      value - possibly null value
      Returns:
      new map reflecting the change
    • update

      @Nonnull public IMap<K,V> update(@Nonnull K key, @Nonnull Func1<Maybe<V>,V> generator)
      Description copied from interface: IMap
      Update the value at the key. A Holder containing the value currently stored at the key, or an empty Holder if the key is not currently bound, is passed to the generator function.
      Specified by:
      update in interface IMap<T,K>
      Parameters:
      key - non-null key
      generator - function to call with current value to create value if key is already bound
      Returns:
      new map with changes applied
    • delete

      @Nonnull public IMap<K,V> delete(@Nonnull K key)
      Description copied from interface: IMap
      Deletes the entry for the specified key (if any). Returns a new map if the value was deleted or the current map if the key was not contained in the map.
      Specified by:
      delete in interface IMap<T,K>
      Parameters:
      key - non-null key
      Returns:
      same or different map depending on whether key was removed
    • size

      public int size()
      Specified by:
      size in interface ICollection<T>
      Returns:
      number of values in the collection
    • deleteAll

      @Nonnull public IMap<K,V> deleteAll()
      Specified by:
      deleteAll in interface ICollection<T>
      Specified by:
      deleteAll in interface IMap<T,K>
      Returns:
      an equivalent collection with no values
    • iterator

      @Nonnull public SplitableIterator<IMapEntry<K,V>> iterator()
      Description copied from interface: IStreamable
      Overridden here to require implementations to return a SplitableIterator rather than a basic Iterator. This is necessary to allow composition of new objects from methods like keys() and values().
      Specified by:
      iterator in interface IStreamable<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface SplitableIterable<T>
    • forEach

      public void forEach(@Nonnull Proc2<K,V> proc)
      Description copied from interface: IMap
      Processes every key/value pair in this map using the provided function.
      Specified by:
      forEach in interface IMap<T,K>
    • forEachThrows

      public <E extends Exception> void forEachThrows(@Nonnull Proc2Throws<K,V,E> proc) throws E
      Description copied from interface: IMap
      Processes every key/value pair in this map using the provided function.
      Specified by:
      forEachThrows in interface IMap<T,K>
      Throws:
      E
    • reduce

      public <R> R reduce(R startingSum, @Nonnull Sum2<K,V,R> proc)
      Description copied from interface: IMap
      Processes every key value pair in this map using the provided function to produce a value.
      Specified by:
      reduce in interface IMap<T,K>
      Type Parameters:
      R - type of the sum
      Parameters:
      startingSum - initial value for process (used with first key/value pair of map)
      proc - function to combine a sum with a key value pair to produce a new sum
      Returns:
      final value (or initial value if this map is empty)
    • reduceThrows

      public <R, E extends Exception> R reduceThrows(R startingSum, @Nonnull Sum2Throws<K,V,R,E> proc) throws E
      Description copied from interface: IMap
      Processes every key value pair in this map using the provided function to produce a value.
      Specified by:
      reduceThrows in interface IMap<T,K>
      Type Parameters:
      R - type of the sum
      E - type of the Exception thrown by the function
      Parameters:
      startingSum - initial value for process (used with first key/value pair of map)
      proc - function to combine a sum with a key value pair to produce a new sum
      Returns:
      final value (or initial value if this map is empty)
      Throws:
      E
    • checkInvariants

      public void checkInvariants()
      Description copied from interface: InvariantCheckable
      Checks invariants of implementing class.
      Specified by:
      checkInvariants in interface InvariantCheckable
    • mappedGetValueOr

      public V mappedGetValueOr(@Nonnull ArrayMapNode<K,V> mapping, @Nonnull K key, V defaultValue)
      Specified by:
      mappedGetValueOr in interface ArrayGetMapper<T,K,V>
    • mappedFind

      @Nonnull public Maybe<V> mappedFind(@Nonnull ArrayMapNode<K,V> mapping, @Nonnull K key)
      Specified by:
      mappedFind in interface ArrayGetMapper<T,K,V>
    • mappedFindEntry

      @Nonnull public Maybe<IMapEntry<K,V>> mappedFindEntry(@Nonnull ArrayMapNode<K,V> mapping, @Nonnull K key)
      Specified by:
      mappedFindEntry in interface ArrayFindEntryMapper<T,K,V>
    • mappedAssign

      @Nonnull public ArrayMapNode<K,V> mappedAssign(@Nonnull K key, V value)
      Description copied from interface: ArrayAssignMapper
      Called during assign operation to create a new mapping for the given key and value.
      Specified by:
      mappedAssign in interface ArrayAssignMapper<T,K,V>
      Parameters:
      key - key being assigned to
      value - value being assigned
      Returns:
      non-null mapping
    • mappedAssign

      @Nonnull public ArrayMapNode<K,V> mappedAssign(@Nonnull ArrayMapNode<K,V> current, @Nonnull K key, V value)
      Description copied from interface: ArrayAssignMapper
      Called during assign operation to replace an existing mapping for the given key and value.
      Specified by:
      mappedAssign in interface ArrayAssignMapper<T,K,V>
      Parameters:
      current - mapping to be replaced
      key - key being assigned to
      value - value being assigned
      Returns:
      same to keep mapping or non-null to replace mapping
    • mappedUpdate

      @Nonnull public ArrayMapNode<K,V> mappedUpdate(@Nonnull ArrayMapNode<K,V> current, @Nonnull K key, @Nonnull Func1<Maybe<V>,V> generator)
      Specified by:
      mappedUpdate in interface ArrayUpdateMapper<T,K,V>
    • mappedDelete

      @Nullable public ArrayMapNode<K,V> mappedDelete(@Nonnull ArrayMapNode<K,V> current, @Nonnull K key)
      Description copied from interface: ArrayDeleteMapper
      Called during delete operation to delete a key from a mapping.
      Specified by:
      mappedDelete in interface ArrayDeleteMapper<T,K>
      Parameters:
      current - mapping to be replaced
      key - key being deleted
      Returns:
      null to remove mapping, same to keep mapping, or non-null to replace mapping
    • mappedSize

      public int mappedSize(@Nonnull ArrayMapNode<K,V> mapping)
      Description copied from interface: ArraySizeMapper
      Called to obtain number of keys in a given mapping.
      Specified by:
      mappedSize in interface ArraySizeMapper<T>
      Parameters:
      mapping - mapping to be sized
      Returns:
      number of keys in the mapping
    • mappedKeys

      @Nonnull public GenericIterator.Iterable<K> mappedKeys(@Nonnull ArrayMapNode<K,V> mapping)
      Specified by:
      mappedKeys in interface ArrayIterationMapper<T,K,V>
    • mappedValues

      @Nonnull public GenericIterator.Iterable<V> mappedValues(@Nonnull ArrayMapNode<K,V> mapping)
      Specified by:
      mappedValues in interface ArrayIterationMapper<T,K,V>
    • mappedEntries

      @Nonnull public GenericIterator.Iterable<IMapEntry<K,V>> mappedEntries(@Nonnull ArrayMapNode<K,V> mapping)
      Specified by:
      mappedEntries in interface ArrayIterationMapper<T,K,V>