Interface SetMultiMap<K,V>


public interface SetMultiMap<K,V>
A thread-safe multimap that associates each key with a Set of values, ensuring uniqueness.

for example

SetMultiMap<Integer, String> map = SetMultiMap.of();
map.put(1, "a");
map.put(1, "a");
map.put(1, "b");

map.size(); // size == 1
map.sizeValue(); // sizeValue == 2

Set<String> set2 = map.get(2); // is null
Set<String> set2 = map.of(2); // is empty set

set2.add("2 - a");
set2.add("2 - a");

map.sizeValue(); // sizeValue == 3

map.containsValue("a"); // true
map.containsValue("b"); // true

var collection = map.clearAll(1);
collection.isEmpty(); // true
map.size(); // size == 2

Set<Integer> keySet = this.map.keySet();

Author:
渔民小镇
date:
2023-12-07
  • Method Summary

    Modifier and Type
    Method
    Description
     
    default void
    Remove all key-collection mappings from this multimap.
    default Collection<V>
    clearAll(K key)
    Clear all elements within the collection corresponding to the key.
    default boolean
    Check whether this multimap contains a mapping for the specified key.
    default boolean
    Check whether any collection in this multimap contains the specified value.
    static <K,V> SetMultiMap<K,V>
    Create a SetMultiMap (framework internal implementation).
    Return a set view of the key-set mappings contained in this multimap.
    default Set<V>
    get(K key)
     
    default boolean
    Check whether this multimap contains no key-collection mappings.
    default Set<K>
    Return the set of keys that have at least one associated value.
    static <K,V> SetMultiMap<K,V>
    of()
    Create a SetMultiMap (framework internal implementation)
    default Set<V>
    of(K key)
     
    ofIfAbsent(K key, Consumer<Set<V>> consumer)
    Get an element collection by key, creating the collection if it does not exist.
    default boolean
    put(K key, V value)
    Add an element to the collection associated with the specified key.
    default int
    The number of key-collection mappings in the map
    default int
    The total number of all values across all collections in the map.
  • Method Details

    • asMap

      Map<K,Set<V>> asMap()
    • ofIfAbsent

      Set<V> ofIfAbsent(K key, Consumer<Set<V>> consumer)
      Get an element collection by key, creating the collection if it does not exist.
      The returned element collection is guaranteed to be non-null; a new one will be created if absent.
      
      The first time the collection for a key is created, the consumer will be called with the newly created collection.
      
      Developers can use the consumer for any necessary initialization.
      
      Parameters:
      key - key
      consumer - consumer
      Returns:
      the collection
    • of

      default Set<V> of(K key)
    • get

      default Set<V> get(K key)
    • entrySet

      Set<Map.Entry<K,Set<V>>> entrySet()
      Return a set view of the key-set mappings contained in this multimap.
      Returns:
      a set of map entries where each value is a Set of elements
    • create

      static <K,V> SetMultiMap<K,V> create()
      Create a SetMultiMap (framework internal implementation). Please use of() instead.
      Type Parameters:
      K - k
      V - v
      Returns:
      SetMultiMap
    • of

      static <K,V> SetMultiMap<K,V> of()
      Create a SetMultiMap (framework internal implementation)
      Type Parameters:
      K - k
      V - v
      Returns:
      SetMultiMap
    • clearAll

      default Collection<V> clearAll(K key)
      Clear all elements within the collection corresponding to the key.
      Parameters:
      key - key
      Returns:
      the collection after clearing, guaranteed to be non-null
    • size

      default int size()
      The number of key-collection mappings in the map
      Returns:
      size
    • sizeValue

      default int sizeValue()
      The total number of all values across all collections in the map.
      Returns:
      value size
    • put

      default boolean put(K key, V value)
      Add an element to the collection associated with the specified key.
      Parameters:
      key - key
      value - element
      Returns:
      true if this collection changed as a result of the call
    • isEmpty

      default boolean isEmpty()
      Check whether this multimap contains no key-collection mappings.
      Returns:
      true if this multimap contains no mappings
    • containsKey

      default boolean containsKey(K key)
      Check whether this multimap contains a mapping for the specified key.
      Parameters:
      key - the key to look up
      Returns:
      true if this multimap contains at least one mapping for the key
    • containsValue

      default boolean containsValue(V value)
      Check whether any collection in this multimap contains the specified value.
      Parameters:
      value - the value to search for across all collections
      Returns:
      true if the value is found in any collection
    • clear

      default void clear()
      Remove all key-collection mappings from this multimap.
    • keySet

      default Set<K> keySet()
      Return the set of keys that have at least one associated value.
      Returns:
      a set view of the keys contained in this multimap