Class HashSetMap<K,V>

java.lang.Object
org.javimmutable.collections.setmap.HashSetMap<K,V>
All Implemented Interfaces:
Serializable, Iterable<IMapEntry<K,ISet<V>>>, ICollection<IMapEntry<K,ISet<V>>>, InvariantCheckable, ISetMap<K,V>, IStreamable<IMapEntry<K,ISet<V>>>, Mapped<K,ISet<V>>, SplitableIterable<IMapEntry<K,ISet<V>>>

@Immutable public class HashSetMap<K,V> extends Object implements Serializable
ISetMap using a hash map for fast lookup.
See Also:
  • Field Details

    • emptySet

      protected final ISet<V> emptySet
    • contents

      protected final IMap<K,ISet<V>> contents
  • Method Details

    • of

      public static <K, V> HashSetMap<K,V> of()
    • checkInvariants

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

      protected ISetMap<K,V> create(IMap<K,ISet<V>> map)
      Implemented by derived classes to create a new instance of the appropriate class.
    • getSet

      @Nonnull public ISet<V> getSet(@Nonnull K key)
      Description copied from interface: ISetMap
      Return the set associated with key, or an empty set if no list is associated.
      Specified by:
      getSet in interface ISetMap<K,V>
    • assign

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

      @Nonnull public ISetMap<K,V> insert(@Nonnull K key, @Nonnull V value)
      Description copied from interface: ISetMap
      Add value to the Set for the specified key. Note that if the value has already been added, it will not be added again.
      Specified by:
      insert in interface ISetMap<K,V>
    • insertAll

      @Nonnull public ISetMap<K,V> insertAll(@Nonnull K key, @Nonnull Iterable<? extends V> values)
      Description copied from interface: ISetMap
      Adds all of the elements of the specified Iterable to the Set for the specified key.
      Specified by:
      insertAll in interface ISetMap<K,V>
    • insertAll

      @Nonnull public ISetMap<K,V> insertAll(@Nonnull K key, @Nonnull Iterator<? extends V> values)
      Description copied from interface: ISetMap
      Adds all of the elements of the specified collection to the Set for the specified key.
      Specified by:
      insertAll in interface ISetMap<K,V>
    • insertAll

      @Nonnull public ISetMap<K,V> insertAll(@Nonnull Iterator<? extends IMapEntry<K,ISet<V>>> iterator)
      Description copied from interface: ICollection
      Add all values to the container in some manner appropriate to the implementation.
      Specified by:
      insertAll in interface ICollection<K>
      Specified by:
      insertAll in interface ISetMap<K,V>
    • contains

      public boolean contains(@Nonnull K key)
      Description copied from interface: ISetMap
      Determines if the setmap contains the specified key.
      Specified by:
      contains in interface ISetMap<K,V>
    • contains

      public boolean contains(@Nonnull K key, @Nullable V value)
      Description copied from interface: ISetMap
      Determines if the Set at key contains the specified value.
      Specified by:
      contains in interface ISetMap<K,V>
      Returns:
      true if the Set contains the value
    • containsAll

      public boolean containsAll(@Nonnull K key, @Nonnull Iterable<? extends V> values)
      Description copied from interface: ISetMap
      Determines if the Set at key contains all values in the specified collection.
      Specified by:
      containsAll in interface ISetMap<K,V>
      Returns:
      true if the Set contains the values
    • containsAll

      public boolean containsAll(@Nonnull K key, @Nonnull Iterator<? extends V> values)
      Description copied from interface: ISetMap
      Determines if the Set at key contains all values in the specified collection.
      Specified by:
      containsAll in interface ISetMap<K,V>
      Returns:
      true if the Set contains the values
    • containsAny

      public boolean containsAny(@Nonnull K key, @Nonnull Iterable<? extends V> values)
      Description copied from interface: ISetMap
      Determines if the Set at key contains any values in the specified collection.
      Specified by:
      containsAny in interface ISetMap<K,V>
      Returns:
      true if the Set contains a value
    • containsAny

      public boolean containsAny(@Nonnull K key, @Nonnull Iterator<? extends V> values)
      Description copied from interface: ISetMap
      Determines if the Set at key contains any values in the specified collection.
      Specified by:
      containsAny in interface ISetMap<K,V>
      Returns:
      true if the Set contains a value
    • delete

      @Nonnull public ISetMap<K,V> delete(@Nonnull K key)
      Description copied from interface: ISetMap
      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 ISetMap<K,V>
    • delete

      @Nonnull public ISetMap<K,V> delete(@Nonnull K key, @Nonnull V value)
      Description copied from interface: ISetMap
      Deletes the specified value from the specified key's set. 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 ISetMap<K,V>
    • deleteAll

      @Nonnull public ISetMap<K,V> deleteAll(@Nonnull K key, @Nonnull Iterable<? extends V> other)
      Description copied from interface: ISetMap
      Deletes the elements in other at the specified key. Returns a new map if the values were deleted or the current map if the key was not contained in the map.
      Specified by:
      deleteAll in interface ISetMap<K,V>
    • deleteAll

      @Nonnull public ISetMap<K,V> deleteAll(@Nonnull K key, @Nonnull Iterator<? extends V> other)
      Description copied from interface: ISetMap
      Deletes the elements in other at the specified key. Returns a new map if the values were deleted or the current map if the key was not contained in the map.
      Specified by:
      deleteAll in interface ISetMap<K,V>
    • union

      @Nonnull public ISetMap<K,V> union(@Nonnull K key, @Nonnull Iterable<? extends V> other)
      Description copied from interface: ISetMap
      Adds all values from other to the Set at key
      Specified by:
      union in interface ISetMap<K,V>
    • union

      @Nonnull public ISetMap<K,V> union(@Nonnull K key, @Nonnull Iterator<? extends V> other)
      Description copied from interface: ISetMap
      Adds all values from other to the Set at key
      Specified by:
      union in interface ISetMap<K,V>
    • intersection

      @Nonnull public ISetMap<K,V> intersection(@Nonnull K key, @Nonnull Iterable<? extends V> other)
      Description copied from interface: ISetMap
      Removes all values from the Set at key that are not contained in the other collection. If the given key is not present in the map, an empty set is added to the map.
      Specified by:
      intersection in interface ISetMap<K,V>
    • intersection

      @Nonnull public ISetMap<K,V> intersection(@Nonnull K key, @Nonnull Iterator<? extends V> other)
      Description copied from interface: ISetMap
      Removes all values from the Set at key that are not contained in the other collection. If the given key is not present in the map, an empty set is added to the map.
      Specified by:
      intersection in interface ISetMap<K,V>
    • intersection

      @Nonnull public ISetMap<K,V> intersection(@Nonnull K key, @Nonnull ISet<? extends V> other)
      Description copied from interface: ISetMap
      Removes all values from the Set at key that are not contained in the other collection. If the given key is not present in the map, an empty set is added to the map.
      Specified by:
      intersection in interface ISetMap<K,V>
    • intersection

      @Nonnull public ISetMap<K,V> intersection(@Nonnull K key, @Nonnull Set<? extends V> other)
      Description copied from interface: ISetMap
      Removes all values from the Set at key that are not contained in the other collection. If the given key is not present in the map, an empty set is added to the map.
      Specified by:
      intersection in interface ISetMap<K,V>
    • size

      public int size()
      Description copied from interface: ISetMap
      Return the number of keys in the map.
      Specified by:
      size in interface ICollection<K>
      Specified by:
      size in interface ISetMap<K,V>
      Returns:
      number of values in the collection
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface ICollection<K>
      Returns:
      true only if collection contains no values
    • insert

      @Nonnull public ISetMap<K,V> insert(@Nonnull IMapEntry<K,ISet<V>> e)
      Description copied from interface: ISetMap
      Add value to the Set for the specified key. Note that if the value has already been added, it will not be added again.
      Specified by:
      insert in interface ICollection<K>
      Specified by:
      insert in interface ISetMap<K,V>
    • iterator

      @Nonnull public SplitableIterator<IMapEntry<K,ISet<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>
      Returns:
      characteristics value used when creating Spliterators
    • keys

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

      @Nonnull public IStreamable<V> values(@Nonnull K key)
      Description copied from interface: ISetMap
      Creates an IStreamable to access all of the specified key's set. If no set exists for key an empty IStreamable is returned.
      Specified by:
      values in interface ISetMap<K,V>
      Returns:
      a (possibly empty) IStreamable for traversing the values associated with key
    • get

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

      public ISet<V> getValueOr(K key, ISet<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<ISet<V>> find(K key)
      Description copied from interface: Mapped
      Return a Holder containing the value associated wth the key or an empty Holder if no value is associated with the key.
      Specified by:
      find in interface Mapped<K,V>
      Parameters:
      key - identifies the value to retrieve
      Returns:
      possibly empty Holder containing any value associated with the key
    • deleteAll

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

      @Nonnull public IStreamable<IMapEntry<K,ISet<V>>> entries()
      Description copied from interface: ISetMap
      Creates an IStreamable to access all of the Map's entries.
      Specified by:
      entries in interface ISetMap<K,V>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

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

      protected void checkSetMapInvariants()