Interface TwoKeyMap<K1,K2,V>

Type Parameters:
K1 - type of first keys in this map
K2 - type of second keys in this map
V - type of values in this map
All Known Implementing Classes:
AbstractTwoKeyMap, MapMapTwoKeyMap

public interface TwoKeyMap<K1,K2,V>
A collection that maps two-key pairs to values. You can visualize the contents of a two-key map either as a map from keys to second-level maps as values:
  • k1 -> { k2 -> v1, k3 -> v2 }
  • k2 -> { k4 -> v1 }

... or as a single "flattened" collection of key1-key2-value triples:

  • k1, k2 -> v1
  • k1, k3 -> v2
  • k2, k4 -> v1

Note that both null keys and values are not permitted in this map.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final record 
    A map entry (key1-key2-value triple).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all the mappings from this map.
    default V
    computeIfAbsent(K1 key1, K2 key2, BiFunction<K1,K2,V> mapper)
    If the specified key-pair is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null.
    boolean
     
    boolean
    containsKey(K1 key1, K2 key2)
     
    boolean
     
     
    default void
    Performs the given action for all key1-key2-value triples contained in this map.
    get(K1 key1)
     
    get(K1 key1, K2 key2)
     
    default Map<K2,V>
    getOrDefault(K1 key1, Map<K2,V> defaultValue)
     
    default V
    getOrDefault(K1 key1, K2 key2, V defaultValue)
     
    boolean
     
     
    put(K1 key1, K2 key2, V value)
    Associates the specified value with the specified two-key pair in this map.
    void
    putAll(K1 key1, Map<K2,V> map)
    Copies all the mappings from the specified map to second-level map associated with key1.
    void
    putAll(TwoKeyMap<K1,K2,V> twoKeyMap)
    Copies all the mappings from the specified two-key map to this map.
    remove(K1 key1, K2 key2)
    Removes the mapping for a key-pair from this map if it is present.
    boolean
    removeAll(K1 key1)
    Removes all mappings with key1 as the first key in this map.
    void
    replaceALl(TriFunction<? super K1,? super K2,? super V,? extends V> function)
    Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
    int
     
     
     
  • Method Details

    • containsKey

      boolean containsKey(K1 key1, K2 key2)
      Returns:
      true if this two-key map contains the mapping with key1 as the first key and key2 as the second key.
    • containsKey

      boolean containsKey(K1 key1)
      Returns:
      true if this two-key map contains at least one mapping with key1 as the first key.
    • containsValue

      boolean containsValue(V value)
      Returns:
      true if this two-key map contains at least one mapping with value as the value. Note that this operation may be slow compared to containsKey(Object).
    • get

      @Nullable V get(K1 key1, K2 key2)
      Returns:
      the value to which the specified keys is mapped, or null if this map contains no mapping for the keys.
    • get

      @Nullable Map<K2,V> get(K1 key1)
      Returns:
      an unmodifiable view of the second-level map for key1, or null if this map contains no mapping for the key.
    • put

      @Nullable V put(@Nonnull K1 key1, @Nonnull K2 key2, @Nonnull V value)
      Associates the specified value with the specified two-key pair in this map. If the map previously contained a mapping for the keys, the old value is replaced by the specified value.
      Returns:
      the previous value associated with key1 and key2, or null if there was no mapping for key2.
    • putAll

      void putAll(@Nonnull K1 key1, @Nonnull Map<K2,V> map)
      Copies all the mappings from the specified map to second-level map associated with key1.
    • putAll

      void putAll(@Nonnull TwoKeyMap<K1,K2,V> twoKeyMap)
      Copies all the mappings from the specified two-key map to this map.
    • remove

      @Nullable V remove(K1 key1, K2 key2)
      Removes the mapping for a key-pair from this map if it is present.
      Returns:
      the value to which this map previously associated the key pair, or null if the map contained no mapping for the key pair.
    • removeAll

      boolean removeAll(K1 key1)
      Removes all mappings with key1 as the first key in this map.
      Returns:
      true if the two-key map changed.
    • replaceALl

      void replaceALl(TriFunction<? super K1,? super K2,? super V,? extends V> function)
      Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
    • twoKeySet

      Set<Pair<K1,K2>> twoKeySet()
      Returns:
      an unmodifiable view of all distinct two-key pairs contained in this two-key map. Note that the result contains a two-key pair if and only if this map maps that key pair to a non-null value.
    • keySet

      Set<K1> keySet()
      Returns:
      an unmodifiable view of first keys of all mappings contained in this two-key map. Note that the result contains a key if and only if this map contains at least one mapping with the key as the first key.
    • values

      Collection<V> values()
      Returns:
      an unmodifiable view collection containing the value from each key1-key2-value triples contained in this map, without collapsing duplicates (so values().size() == size()).
    • entrySet

      Set<TwoKeyMap.Entry<K1,K2,V>> entrySet()
      Returns:
      an unmodifiable view of all key1-key2-value triples contained in this two-key map, as TwoKeyMap.Entry instances.
    • forEach

      default void forEach(@Nonnull TriConsumer<K1,K2,V> action)
      Performs the given action for all key1-key2-value triples contained in this map.
    • getOrDefault

      default Map<K2,V> getOrDefault(K1 key1, Map<K2,V> defaultValue)
      Returns:
      the k2-value map to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
    • getOrDefault

      default V getOrDefault(K1 key1, K2 key2, V defaultValue)
      Returns:
      the value to which the specified key-pair is mapped, or defaultValue if this map contains no mapping for the key-pair.
    • computeIfAbsent

      default V computeIfAbsent(K1 key1, K2 key2, @Nonnull BiFunction<K1,K2,V> mapper)
      If the specified key-pair is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null.

      If the mapping function returns null, no mapping is recorded. If the mapping function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded.

      Returns:
      the current (existing or computed) value associated with the specified key, or null if the computed value is null.
    • clear

      void clear()
      Removes all the mappings from this map. The map will be empty after this call returns.
    • isEmpty

      boolean isEmpty()
      Returns:
      true if this map contains no key1-key2-value mappings.
    • size

      int size()
      Returns:
      the number of key1-key2-value mappings in this map.