Class Multimap<K,E,V extends Collection<E>>

java.lang.Object
com.landawn.abacus.util.Multimap<K,E,V>
Type Parameters:
K - the key type
E -
V - the value type
Direct Known Subclasses:
ListMultimap, SetMultimap

public class Multimap<K,E,V extends Collection<E>> extends Object
Similar to Map, but in which each key may be associated with multiple values.
  • a ->1, 2
  • b -> 3
Since:
0.8
Author:
Haiyang Li
See Also:
  • CommonUtil.newMultimap(Supplier, Supplier)
  • CommonUtil.newListMultimap()
  • CommonUtil.newListMultimap(Class, Class)
  • CommonUtil.newListMultimap(Supplier, Supplier)
  • CommonUtil.newSetMultimap()
  • CommonUtil.newSetMultimap(Class, Class)
  • CommonUtil.newSetMultimap(Supplier, Supplier)
  • Method Details

    • get

      public V get(Object key)
      Parameters:
      key -
      Returns:
    • getOrDefault

      public V getOrDefault(Object key, V defaultValue)
      Gets the or default.
      Parameters:
      key -
      defaultValue -
      Returns:
    • put

      public boolean put(K key, E e)
      Parameters:
      key -
      e -
      Returns:
    • putIfAbsent

      public boolean putIfAbsent(K key, E e)
      If the specified value is not already associated with the specified key associates it with the given key and returns true, else returns false.
      Parameters:
      key -
      e -
      Returns:
    • putIfKeyAbsent

      public boolean putIfKeyAbsent(K key, E e)
      If the specified key is not already associated with any value (or is mapped to null) associates it with the given value and returns true, else returns false.
      Parameters:
      key -
      e -
      Returns:
    • putAll

      public boolean putAll(K key, Collection<? extends E> c)
      Parameters:
      key -
      c -
      Returns:
    • putAllIfKeyAbsent

      public boolean putAllIfKeyAbsent(K key, Collection<? extends E> c)
      If the specified key is not already associated with any value (or is mapped to null) associates it with the given values in the specified collection and returns true, else returns false.
      Parameters:
      key -
      c -
      Returns:
    • putAll

      public boolean putAll(Map<? extends K,? extends E> m)
      Parameters:
      m -
      Returns:
    • putAll

      public boolean putAll(Multimap<? extends K,? extends E,? extends Collection<? extends E>> m)
      Parameters:
      m -
      Returns:
    • remove

      public boolean remove(Object key, Object e)
      Parameters:
      key -
      e -
      Returns:
    • removeAll

      public V removeAll(Object key)
      Removes the all.
      Parameters:
      key -
      Returns:
      values associated with specified key.
    • removeAll

      public boolean removeAll(Object key, Collection<?> c)
      Removes the all.
      Parameters:
      key -
      c -
      Returns:
    • removeAll

      public boolean removeAll(Map<?,? extends Collection<?>> m)
       
       ListMultimap<String, Integer> listMultimap = ListMultimap.of("a", 1, "b", 2, "a", 2, "a", 2); // -> {a=[1, 2, 2], b=[2]}
       listMultimap.removeAll(N.asMap("a", N.asList(2))); // -> {a=[1], b=[2]}
       
       
      Parameters:
      m -
      Returns:
    • removeAll

      public boolean removeAll(Multimap<?,?,?> m)
      Removes the all.
      Parameters:
      m -
      Returns:
    • removeIf

      public <X extends Exception> boolean removeIf(E value, Throwables.Predicate<? super K,X> predicate) throws X
      Remove the specified value (one occurrence) from the value set associated with keys which satisfy the specified predicate.
      Type Parameters:
      X -
      Parameters:
      value -
      predicate -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • removeIf

      public <X extends Exception> boolean removeIf(E value, Throwables.BiPredicate<? super K,? super V,X> predicate) throws X
      Remove the specified value (one occurrence) from the value set associated with keys which satisfy the specified predicate.
      Type Parameters:
      X -
      Parameters:
      value -
      predicate -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • removeAllIf

      public <X extends Exception> boolean removeAllIf(Collection<?> values, Throwables.Predicate<? super K,X> predicate) throws X
      Remove the specified values (all occurrences) from the value set associated with keys which satisfy the specified predicate.
      Type Parameters:
      X -
      Parameters:
      values -
      predicate -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • removeAllIf

      public <X extends Exception> boolean removeAllIf(Collection<?> values, Throwables.BiPredicate<? super K,? super V,X> predicate) throws X
      Remove the specified values (all occurrences) from the value set associated with keys which satisfy the specified predicate.
      Type Parameters:
      X -
      Parameters:
      values -
      predicate -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • removeAllIf

      public <X extends Exception> boolean removeAllIf(Throwables.Predicate<? super K,X> predicate) throws X
      Remove all the values associated with keys which satisfy the specified predicate.
      Type Parameters:
      X -
      Parameters:
      predicate -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • removeAllIf

      public <X extends Exception> boolean removeAllIf(Throwables.BiPredicate<? super K,? super V,X> predicate) throws X
      Remove all the values associated with keys which satisfy the specified predicate.
      Type Parameters:
      X -
      Parameters:
      predicate -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • replace

      public boolean replace(K key, E oldValue, E newValue)
      Replace the specified oldValue (one occurrence) with the specified newValue. False is returned if no oldValue is found.
      Parameters:
      key -
      oldValue -
      newValue -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
    • replaceAll

      public boolean replaceAll(K key, E oldValue, E newValue)
      Replace the specified oldValue (all occurrences) with the specified newValue. False is returned if no oldValue is found.
      Parameters:
      key -
      oldValue -
      newValue -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
    • replaceAll

      @Deprecated public boolean replaceAll(K key, Collection<? extends E> oldValues, E newValue)
      Deprecated.
      Replace all the specified oldValues (all occurrences) with single specified newValue. False is returned if no oldValue is found.
      Parameters:
      key -
      oldValues -
      newValue -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
    • replaceIf

      public <X extends Exception> boolean replaceIf(Throwables.Predicate<? super K,X> predicate, E oldValue, E newValue) throws X
      Replace the specified oldValue (one occurrence) from the value set associated with keys which satisfy the specified predicate with the specified newValue.
      Type Parameters:
      X -
      Parameters:
      predicate -
      oldValue -
      newValue -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • replaceIf

      public <X extends Exception> boolean replaceIf(Throwables.BiPredicate<? super K,? super V,X> predicate, E oldValue, E newValue) throws X
      Replace the specified oldValue (one occurrence) from the value set associated with keys which satisfy the specified predicate with the specified newValue.
      Type Parameters:
      X -
      Parameters:
      predicate -
      oldValue -
      newValue -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • replaceAllIf

      public <X extends Exception> boolean replaceAllIf(Throwables.Predicate<? super K,X> predicate, E oldValue, E newValue) throws X
      Replace the specified oldValue (all occurrences) from the value set associated with keys which satisfy the specified predicate with the specified newValue.
      Type Parameters:
      X -
      Parameters:
      predicate -
      oldValue -
      newValue -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • replaceAllIf

      public <X extends Exception> boolean replaceAllIf(Throwables.BiPredicate<? super K,? super V,X> predicate, E oldValue, E newValue) throws X
      Replace the specified oldValue (all occurrences) from the value set associated with keys which satisfy the specified predicate with the specified newValue.
      Type Parameters:
      X -
      Parameters:
      predicate -
      oldValue -
      newValue -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • replaceAllIf

      @Deprecated public <X extends Exception> boolean replaceAllIf(Throwables.Predicate<? super K,X> predicate, Collection<? extends E> oldValues, E newValue) throws X
      Deprecated.
      Replace all the specified oldValue (all occurrences) from the value set associated with keys which satisfy the specified predicate with single specified newValue.
      Type Parameters:
      X -
      Parameters:
      predicate -
      oldValues -
      newValue -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • replaceAllIf

      @Deprecated public <X extends Exception> boolean replaceAllIf(Throwables.BiPredicate<? super K,? super V,X> predicate, Collection<? extends E> oldValues, E newValue) throws X
      Deprecated.
      Replace all the specified oldValue (all occurrences) from the value set associated with keys which satisfy the specified predicate with single specified newValue.
      Type Parameters:
      X -
      Parameters:
      predicate -
      oldValues -
      newValue -
      Returns:
      true if this Multimap is modified by this operation, otherwise false.
      Throws:
      X - the x
    • replaceAll

      public <X extends Exception> void replaceAll(Throwables.BiFunction<? super K,? super V,? extends V,X> function) throws X
      The associated keys will be removed if null or empty values are returned by the specified function.
      Type Parameters:
      X -
      Parameters:
      function -
      Throws:
      X - the x
    • contains

      public boolean contains(Object key, Object e)
      Parameters:
      key -
      e -
      Returns:
    • containsKey

      public boolean containsKey(Object key)
      Parameters:
      key -
      Returns:
    • containsValue

      public boolean containsValue(Object e)
      Parameters:
      e -
      Returns:
    • containsAll

      public boolean containsAll(Object key, Collection<?> c)
      Parameters:
      key -
      c -
      Returns:
    • filterByKey

      public <X extends Exception> Multimap<K,E,V> filterByKey(Throwables.Predicate<? super K,X> filter) throws X
      Filter by key.
      Type Parameters:
      X -
      Parameters:
      filter -
      Returns:
      Throws:
      X - the x
    • filterByValue

      public <X extends Exception> Multimap<K,E,V> filterByValue(Throwables.Predicate<? super V,X> filter) throws X
      Filter by value.
      Type Parameters:
      X -
      Parameters:
      filter -
      Returns:
      Throws:
      X - the x
    • filter

      public <X extends Exception> Multimap<K,E,V> filter(Throwables.BiPredicate<? super K,? super V,X> filter) throws X
      Type Parameters:
      X -
      Parameters:
      filter -
      Returns:
      Throws:
      X - the x
    • forEach

      public <X extends Exception> void forEach(Throwables.BiConsumer<? super K,? super V,X> action) throws X
      Type Parameters:
      X -
      Parameters:
      action -
      Throws:
      X - the x
    • flatForEach

      public <X extends Exception> void flatForEach(Throwables.BiConsumer<? super K,? super E,X> action) throws X
      Flat for each.
      Type Parameters:
      X -
      Parameters:
      action -
      Throws:
      X - the x
    • forEachKey

      public <X extends Exception> void forEachKey(Throwables.Consumer<? super K,X> action) throws X
      For each key.
      Type Parameters:
      X -
      Parameters:
      action -
      Throws:
      X - the x
    • forEachValue

      public <X extends Exception> void forEachValue(Throwables.Consumer<? super V,X> action) throws X
      For each value.
      Type Parameters:
      X -
      Parameters:
      action -
      Throws:
      X - the x
    • flatForEachValue

      public <X extends Exception> void flatForEachValue(Throwables.Consumer<? super E,X> action) throws X
      Flat for each value.
      Type Parameters:
      X -
      Parameters:
      action -
      Throws:
      X - the x
    • computeIfAbsent

      public <X extends Exception> V computeIfAbsent(K key, Throwables.Function<? super K,? extends V,X> mappingFunction) throws X
      The implementation is equivalent to performing the following steps for this Multimap:
       final V oldValue = get(key);
      
       if (N.notNullOrEmpty(oldValue)) {
           return oldValue;
       }
      
       final V newValue = mappingFunction.apply(key);
      
       if (N.notNullOrEmpty(newValue)) {
           valueMap.put(key, newValue);
       }
      
       return newValue;
       
      Type Parameters:
      X -
      Parameters:
      key -
      mappingFunction -
      Returns:
      Throws:
      X - the x
    • computeIfPresent

      public <X extends Exception> V computeIfPresent(K key, Throwables.BiFunction<? super K,? super V,? extends V,X> remappingFunction) throws X
      The implementation is equivalent to performing the following steps for this Multimap:
       final V oldValue = get(key);
      
       if (N.isNullOrEmpty(oldValue)) {
           return oldValue;
       }
      
       final V newValue = remappingFunction.apply(key, oldValue);
      
       if (N.notNullOrEmpty(newValue)) {
           valueMap.put(key, newValue);
       } else {
           valueMap.remove(key);
       }
      
       return newValue;
       
      Type Parameters:
      X -
      Parameters:
      key -
      remappingFunction -
      Returns:
      Throws:
      X - the x
    • compute

      public <X extends Exception> V compute(K key, Throwables.BiFunction<? super K,? super V,? extends V,X> remappingFunction) throws X
      The implementation is equivalent to performing the following steps for this Multimap:
       final V oldValue = get(key);
       final V newValue = remappingFunction.apply(key, oldValue);
      
       if (N.notNullOrEmpty(newValue)) {
           valueMap.put(key, newValue);
       } else {
           if (oldValue != null) {
               valueMap.remove(key);
           }
       }
      
       return newValue;
       
      Type Parameters:
      X -
      Parameters:
      key -
      remappingFunction -
      Returns:
      Throws:
      X - the x
    • merge

      public <X extends Exception> V merge(K key, V value, Throwables.BiFunction<? super V,? super V,? extends V,X> remappingFunction) throws X
      The implementation is equivalent to performing the following steps for this Multimap:
       final V oldValue = get(key);
       final V newValue = oldValue == null ? value : remappingFunction.apply(oldValue, value);
      
       if (N.notNullOrEmpty(newValue)) {
           valueMap.put(key, newValue);
       } else {
           if (oldValue != null) {
               valueMap.remove(key);
           }
       }
      
       return newValue;
       
      Type Parameters:
      X -
      Parameters:
      key -
      value -
      remappingFunction -
      Returns:
      Throws:
      X - the x
    • merge

      public <X extends Exception> V merge(K key, E e, Throwables.BiFunction<? super V,? super E,? extends V,X> remappingFunction) throws X
      The implementation is equivalent to performing the following steps for this Multimap:
       final V oldValue = get(key);
      
       if (N.isNullOrEmpty(oldValue)) {
           put(key, e);
           return get(key);
       }
      
       final V newValue = remappingFunction.apply(oldValue, e);
      
       if (N.notNullOrEmpty(newValue)) {
           valueMap.put(key, newValue);
       } else {
           if (oldValue != null) {
               valueMap.remove(key);
           }
       }
      
       return newValue;
       
      Type Parameters:
      X -
      Parameters:
      key -
      e -
      remappingFunction -
      Returns:
      Throws:
      X - the x
    • copy

      public Multimap<K,E,V> copy()
      Returns:
    • inverse

      public <VV extends Collection<K>, M extends Multimap<E, K, VV>> M inverse(IntFunction<? extends M> multimapSupplier)
      Type Parameters:
      VV -
      M -
      Parameters:
      multimapSupplier -
      Returns:
    • keySet

      public Set<K> keySet()
      Returns:
    • values

      public Collection<V> values()
      Returns:
    • flatValues

      public List<E> flatValues()
      Returns:
    • flatValues

      public <R extends Collection<E>> R flatValues(IntFunction<R> supplier)
      Type Parameters:
      R -
      Parameters:
      supplier -
      Returns:
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Returns:
    • toMap

      public Map<K,V> toMap()
      Returns:
    • toMap

      public <M extends Map<K, V>> M toMap(IntFunction<? extends M> supplier)
      Type Parameters:
      M -
      Parameters:
      supplier -
      Returns:
    • toMultiset

      public Multiset<K> toMultiset()
      Returns:
    • unwrap

      @Beta public Map<K,V> unwrap()
      Returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key's associated values.

      Changes to the returned map or the collections that serve as its values will update the underlying multimap, and vice versa.

      Returns:
    • stream

      public Stream<Map.Entry<K,V>> stream()
      Returns:
    • entryStream

      public EntryStream<K,V> entryStream()
      Returns:
    • clear

      public void clear()
      Clear.
    • size

      public int size()
      Returns:
    • totalCountOfValues

      public int totalCountOfValues()
      Returns the total count of all the elements in all values.
      Returns:
    • isEmpty

      public boolean isEmpty()
      Checks if is empty.
      Returns:
      true, if is empty
    • apply

      public <R, X extends Exception> R apply(Throwables.Function<? super Multimap<K,E,V>,R,X> func) throws X
      Type Parameters:
      R -
      X -
      Parameters:
      func -
      Returns:
      Throws:
      X - the x
    • applyIfNotEmpty

      public <R, X extends Exception> u.Optional<R> applyIfNotEmpty(Throwables.Function<? super Multimap<K,E,V>,R,X> func) throws X
      Type Parameters:
      R -
      X -
      Parameters:
      func -
      Returns:
      Throws:
      X - the x
    • accept

      public <X extends Exception> void accept(Throwables.Consumer<? super Multimap<K,E,V>,X> action) throws X
      Type Parameters:
      X -
      Parameters:
      action -
      Throws:
      X - the x
    • acceptIfNotEmpty

      public <X extends Exception> If.OrElse acceptIfNotEmpty(Throwables.Consumer<? super Multimap<K,E,V>,X> action) throws X
      Accept if not empty.
      Type Parameters:
      X -
      Parameters:
      action -
      Returns:
      Throws:
      X - the x
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
      Parameters:
      obj -
      Returns:
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns: