Class TreeMap<K,V>

java.lang.Object
org.javimmutable.collections.common.AbstractMap<K,V>
org.javimmutable.collections.tree.TreeMap<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 TreeMap<K,V> extends AbstractMap<K,V> implements Serializable
See Also:
  • Method Details

    • of

      @Nonnull public static <K extends Comparable<K>, V> TreeMap<K,V> of()
    • of

      @Nonnull public static <K, V> TreeMap<K,V> of(@Nonnull Comparator<K> comparator)
    • builder

      @Nonnull public static <K extends Comparable<K>, V> IMapBuilder<K,V> builder()
    • builder

      @Nonnull public static <K, V> IMapBuilder<K,V> builder(@Nonnull Comparator<K> comparator)
    • 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 extends Comparable<K>, V> java.util.stream.Collector<IMapEntry<K,V>,?,IMap<K,V>> createMapCollector()
    • createMapCollector

      @Nonnull public static <K, V> java.util.stream.Collector<IMapEntry<K,V>,?,IMap<K,V>> createMapCollector(@Nonnull Comparator<K> comparator)
    • 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 TreeMap<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
    • update

      @Nonnull public TreeMap<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<K,V>
      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 TreeMap<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 TreeMap<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
    • 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
    • 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>
    • 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<K,V>
    • 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<K,V>
      Throws:
      E
    • reduce

      public <R> R reduce(R sum, @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<K,V>
      Type Parameters:
      R - type of the sum
      Parameters:
      sum - 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 sum, @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<K,V>
      Type Parameters:
      R - type of the sum
      E - type of the Exception thrown by the function
      Parameters:
      sum - 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
    • getComparator

      @Nonnull public Comparator<K> getComparator()