Class HashSetMultimap<K,V>

java.lang.Object
be.imgn.gaia.core.collect.HashSetMultimap<K,V>
Type Parameters:
K - the key type
V - the value type
All Implemented Interfaces:
Multimap<K,V>, SetMultimap<K,V>, Serializable

public final class HashSetMultimap<K,V> extends Object implements Serializable
A modifiable set multimap backed by a HashMap from each key to a HashSet of its values. Its keys and values must have consistent equals and hashCode methods.
  • It iterates over its keys, and over the values of a key, in no particular order.
  • It rejects null keys and values, but querying it with null, as in containsKey(null), is allowed.
  • Its views are live, and its iterators support Iterator.remove(). The collections of Multimap.asMap() do not support adding values: add them through Multimap.get(Object) or Multimap.put(K, V).
  • Its iterators fail fast, on a best-effort basis, when it changes while they iterate, outside of their own remove().
  • It is serializable if all its keys and values are.
  • It is not thread-safe.
Since:
0.1.2
See Also:
  • Method Details

    • create

      public static <K,V> HashSetMultimap<K,V> create()
      Returns a new, empty multimap.
      Type Parameters:
      K - the key type
      V - the value type
      Returns:
      a new, empty multimap
    • create

      public static <K,V> HashSetMultimap<K,V> create(Multimap<? extends K, ? extends V> multimap)
      Returns a new multimap containing the entries of another multimap.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      multimap - the multimap whose entries to add
      Returns:
      a new multimap containing the entries of multimap
    • entries

      public Set<Map.Entry<K,V>> entries()
      Description copied from interface: Multimap
      Returns a live view of the entries, grouped by key. The entries do not support Map.Entry.setValue(V).
      Specified by:
      entries in interface Multimap<K,V>
      Specified by:
      entries in interface SetMultimap<K,V>
      Returns:
      the entries of this multimap
    • size

      public int size()
      Description copied from interface: Multimap
      Returns the number of entries: the key-value pairs, not the distinct keys.
      Specified by:
      size in interface Multimap<K,V>
      Returns:
      the number of entries, or Integer.MAX_VALUE if there are more
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Multimap
      Returns whether this multimap has no entries.
      Specified by:
      isEmpty in interface Multimap<K,V>
      Returns:
      true if this multimap is empty
    • containsKey

      public boolean containsKey(@Nullable Object key)
      Description copied from interface: Multimap
      Returns whether a key has at least one value.
      Specified by:
      containsKey in interface Multimap<K,V>
      Parameters:
      key - the key to look for
      Returns:
      true if this multimap contains key
    • containsValue

      public boolean containsValue(@Nullable Object value)
      Description copied from interface: Multimap
      Returns whether a value belongs to at least one key.
      Specified by:
      containsValue in interface Multimap<K,V>
      Parameters:
      value - the value to look for
      Returns:
      true if this multimap contains value
    • containsEntry

      public boolean containsEntry(@Nullable Object key, @Nullable Object value)
      Description copied from interface: Multimap
      Returns whether a key has a value.
      Specified by:
      containsEntry in interface Multimap<K,V>
      Parameters:
      key - the key to look for
      value - the value to look for
      Returns:
      true if this multimap contains an entry with key and value
    • get

      public Set<V> get(K key)
      Description copied from interface: Multimap
      Returns a live view of the values of a key, empty if the key is absent. Adding to the view adds entries to this multimap, the key included if it was absent.
      Specified by:
      get in interface Multimap<K,V>
      Parameters:
      key - the key
      Returns:
      the values of key
    • keySet

      public Set<K> keySet()
      Description copied from interface: Multimap
      Returns a live view of the distinct keys. Removing a key removes all its values.
      Specified by:
      keySet in interface Multimap<K,V>
      Returns:
      the keys of this multimap
    • values

      public Collection<V> values()
      Description copied from interface: Multimap
      Returns a live view of the values of all keys, each value as many times as it occurs, grouped by key.
      Specified by:
      values in interface Multimap<K,V>
      Returns:
      the values of this multimap
    • asMap

      public Map<K,Set<V>> asMap()
      Description copied from interface: Multimap
      Returns a live view of this multimap as a map from each key to its values. The map and its collections support removal, but not put nor adding values.
      Specified by:
      asMap in interface Multimap<K,V>
      Returns:
      this multimap as a map
    • forEach

      public void forEach(BiConsumer<? super K, ? super V> action)
      Description copied from interface: Multimap
      Calls an action with the key and value of each entry, grouped by key.
      Specified by:
      forEach in interface Multimap<K,V>
      Parameters:
      action - the action to call
    • put

      public boolean put(K key, V value)
      Description copied from interface: Multimap
      Adds an entry.
      Specified by:
      put in interface Multimap<K,V>
      Parameters:
      key - the key
      value - the value
      Returns:
      true if this multimap changed, which a SetMultimap does not if it already contains the entry
    • putAll

      public boolean putAll(K key, Iterable<? extends V> values)
      Description copied from interface: Multimap
      Adds an entry for each of the given values, with the same key.
      Specified by:
      putAll in interface Multimap<K,V>
      Parameters:
      key - the key
      values - the values
      Returns:
      true if this multimap changed
    • putAll

      public boolean putAll(Multimap<? extends K, ? extends V> multimap)
      Description copied from interface: Multimap
      Adds the entries of another multimap.
      Specified by:
      putAll in interface Multimap<K,V>
      Parameters:
      multimap - the multimap whose entries to add
      Returns:
      true if this multimap changed
    • remove

      public boolean remove(@Nullable Object key, @Nullable Object value)
      Description copied from interface: Multimap
      Removes an entry. A ListMultimap removes its first occurrence.
      Specified by:
      remove in interface Multimap<K,V>
      Parameters:
      key - the key
      value - the value
      Returns:
      true if this multimap changed
    • removeAll

      public Set<V> removeAll(@Nullable Object key)
      Description copied from interface: Multimap
      Removes all the values of a key.
      Specified by:
      removeAll in interface Multimap<K,V>
      Parameters:
      key - the key
      Returns:
      the values removed, in an unmodifiable collection, empty if the key was absent
    • replaceValues

      public Set<V> replaceValues(K key, Iterable<? extends V> values)
      Description copied from interface: Multimap
      Replaces all the values of a key. Replacing them with no values removes the key.
      Specified by:
      replaceValues in interface Multimap<K,V>
      Parameters:
      key - the key
      values - the new values of key
      Returns:
      the values replaced, in an unmodifiable collection, empty if the key was absent
    • clear

      public void clear()
      Description copied from interface: Multimap
      Removes all entries.
      Specified by:
      clear in interface Multimap<K,V>
    • equals

      public boolean equals(@Nullable Object o)
      Description copied from interface: Multimap
      Returns whether another object is a multimap with the same keys and values. Two multimaps are equal when their Multimap.asMap() views are, so a ListMultimap equals another one with the same values in the same order, and a SetMultimap another one with the same values in any order. A list multimap never equals a set multimap, unless both are empty.
      Specified by:
      equals in interface Multimap<K,V>
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare with
      Returns:
      true if o is an equal multimap
    • hashCode

      public int hashCode()
      Description copied from interface: Multimap
      Returns the hash code of Multimap.asMap().
      Specified by:
      hashCode in interface Multimap<K,V>
      Overrides:
      hashCode in class Object
      Returns:
      the hash code of this multimap
    • toString

      public String toString()
      Overrides:
      toString in class Object