Class OrderedMap<K,V>

java.lang.Object
org.javimmutable.collections.common.AbstractMap<K,V>
org.javimmutable.collections.inorder.OrderedMap<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 OrderedMap<K,V> extends AbstractMap<K,V> implements Serializable
IMap implementation that allows iteration over members in the order in which they were inserted into the map. Maintains two parallel data structures, one for sorting and the other for storing entries. Gets are approximately as fast as hash map gets but updates are significantly slower.
See Also:
  • Field Details

  • Method Details

    • of

      public static <K, V> OrderedMap<K,V> of()
    • builder

      @Nonnull 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<K,V>
    • createMapCollector

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

      @Nonnull public java.util.stream.Collector<IMapEntry<K,V>,?,IMap<K,V>> mapCollector()
      Description copied from interface: IMap
      Returns a Collector that creates a set of the same type as this containing all of the collected values inserted over whatever starting values this already contained.
      Specified by:
      mapCollector in interface IMap<K,V>
    • 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
    • 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 OrderedMap<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 OrderedMap<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 OrderedMap<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>
    • keys

      @Nonnull public IStreamable<K> keys()
      Description copied from interface: IMap
      Creates an IStreamable to access all of the Map's keys.
      Specified by:
      keys in interface IMap<K,V>
      Overrides:
      keys in class AbstractMap<K,V>
    • values

      @Nonnull public IStreamable<V> values()
      Description copied from interface: IMap
      Creates an IStreamable to access all of the Map's values.
      Specified by:
      values in interface IMap<K,V>
      Overrides:
      values in class AbstractMap<K,V>
    • 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
    • checkInvariants

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