Class EmptyHashMap<K,V>

java.lang.Object
org.javimmutable.collections.common.AbstractMap<K,V>
org.javimmutable.collections.hash.EmptyHashMap<K,V>
All Implemented Interfaces:
Serializable, Iterable<IMapEntry<K,V>>, ICollection<IMapEntry<K,V>>, IMap<K,V>, InvariantCheckable, IStreamable<IMapEntry<K,V>>, Mapped<K,V>, SplitableIterable<IMapEntry<K,V>>

@Immutable public class EmptyHashMap<K,V> extends AbstractMap<K,V> implements Serializable
Singleton implementation of IMap that contains no elements. When a value is assigned to the map a
invalid reference
IHashMap
is created that manages hash collisions using a tree if key is Comparable or a list otherwise.
See Also:
  • Method Details

    • 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<K,V>
    • 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<K,V>
      Specified by:
      find in interface Mapped<K,V>
      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<K,V>
      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<K,V>
      Parameters:
      key - non-null key
      value - possibly null value
      Returns:
      new map reflecting the change
    • 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<K,V>
      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<K>
      Returns:
      number of values in the collection
    • deleteAll

      @Nonnull public IMap<K,V> deleteAll()
      Specified by:
      deleteAll in interface ICollection<K>
      Specified by:
      deleteAll in interface IMap<K,V>
      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<K>
      Specified by:
      iterator in interface Iterable<K>
      Specified by:
      iterator in interface SplitableIterable<K>
    • getSpliteratorCharacteristics

      public int getSpliteratorCharacteristics()
      Specified by:
      getSpliteratorCharacteristics in interface IStreamable<K>
      Overrides:
      getSpliteratorCharacteristics in class AbstractMap<K,V>
      Returns:
      characteristics value used when creating Spliterators
    • 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<K,V>
      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
    • checkInvariants

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