Package com.landawn.abacus.util
Class Multimap<K,E,V extends Collection<E>>
java.lang.Object
com.landawn.abacus.util.Multimap<K,E,V>
- Type Parameters:
K
- the key typeE
-V
- the value type
- Direct Known Subclasses:
ListMultimap
,SetMultimap
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 Summary
Modifier and TypeMethodDescription<X extends Exception>
voidacceptIfNotEmpty
(Throwables.Consumer<? super Multimap<K, E, V>, X> action) Accept if not empty.<R,
X extends Exception>
R<R,
X extends Exception>
u.Optional<R>applyIfNotEmpty
(Throwables.Function<? super Multimap<K, E, V>, R, X> func) void
clear()
Clear.The implementation is equivalent to performing the following steps for this Multimap:computeIfAbsent
(K key, Throwables.Function<? super K, ? extends V, X> mappingFunction) The implementation is equivalent to performing the following steps for this Multimap:computeIfPresent
(K key, Throwables.BiFunction<? super K, ? super V, ? extends V, X> remappingFunction) The implementation is equivalent to performing the following steps for this Multimap:boolean
boolean
containsAll
(Object key, Collection<?> c) boolean
containsKey
(Object key) boolean
copy()
entrySet()
boolean
filter
(Throwables.BiPredicate<? super K, ? super V, X> filter) filterByKey
(Throwables.Predicate<? super K, X> filter) Filter by key.filterByValue
(Throwables.Predicate<? super V, X> filter) Filter by value.<X extends Exception>
voidflatForEach
(Throwables.BiConsumer<? super K, ? super E, X> action) Flat for each.<X extends Exception>
voidflatForEachValue
(Throwables.Consumer<? super E, X> action) Flat for each value.<R extends Collection<E>>
RflatValues
(IntFunction<R> supplier) <X extends Exception>
voidforEach
(Throwables.BiConsumer<? super K, ? super V, X> action) <X extends Exception>
voidforEachKey
(Throwables.Consumer<? super K, X> action) For each key.<X extends Exception>
voidforEachValue
(Throwables.Consumer<? super V, X> action) For each value.getOrDefault
(Object key, V defaultValue) Gets the or default.int
hashCode()
<VV extends Collection<K>,
M extends Multimap<E, K, VV>>
Minverse
(IntFunction<? extends M> multimapSupplier) boolean
isEmpty()
Checks if is empty.keySet()
The implementation is equivalent to performing the following steps for this Multimap:The implementation is equivalent to performing the following steps for this Multimap:boolean
boolean
boolean
boolean
putAll
(K key, Collection<? extends E> c) boolean
putAllIfKeyAbsent
(K key, Collection<? extends E> c) If the specified key is not already associated with any value (or is mapped tonull
) associates it with the given values in the specifiedcollection
and returnstrue
, else returnsfalse
.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 returnstrue
, else returnsfalse
.boolean
putIfKeyAbsent
(K key, E e) If the specified key is not already associated with any value (or is mapped tonull
) associates it with the given value and returnstrue
, else returnsfalse
.boolean
boolean
Removes the all.Removes the all.boolean
removeAll
(Object key, Collection<?> c) Removes the all.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]}
<X extends Exception>
booleanremoveAllIf
(Throwables.BiPredicate<? super K, ? super V, X> predicate) Remove all the values associated with keys which satisfy the specifiedpredicate
.<X extends Exception>
booleanremoveAllIf
(Throwables.Predicate<? super K, X> predicate) Remove all the values associated with keys which satisfy the specifiedpredicate
.<X extends Exception>
booleanremoveAllIf
(Collection<?> values, Throwables.BiPredicate<? super K, ? super V, X> predicate) Remove the specified values (all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
.<X extends Exception>
booleanremoveAllIf
(Collection<?> values, Throwables.Predicate<? super K, X> predicate) Remove the specified values (all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
.<X extends Exception>
booleanremoveIf
(E value, Throwables.BiPredicate<? super K, ? super V, X> predicate) Remove the specified value (one occurrence) from the value set associated with keys which satisfy the specifiedpredicate
.<X extends Exception>
booleanremoveIf
(E value, Throwables.Predicate<? super K, X> predicate) Remove the specified value (one occurrence) from the value set associated with keys which satisfy the specifiedpredicate
.boolean
Replace the specifiedoldValue
(one occurrence) with the specifiednewValue
.<X extends Exception>
voidreplaceAll
(Throwables.BiFunction<? super K, ? super V, ? extends V, X> function) The associated keys will be removed if null or empty values are returned by the specifiedfunction
.boolean
replaceAll
(K key, E oldValue, E newValue) Replace the specifiedoldValue
(all occurrences) with the specifiednewValue
.boolean
replaceAll
(K key, Collection<? extends E> oldValues, E newValue) Deprecated.<X extends Exception>
booleanreplaceAllIf
(Throwables.BiPredicate<? super K, ? super V, X> predicate, E oldValue, E newValue) Replace the specifiedoldValue
(all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
with the specifiednewValue
.<X extends Exception>
booleanreplaceAllIf
(Throwables.BiPredicate<? super K, ? super V, X> predicate, Collection<? extends E> oldValues, E newValue) Deprecated.<X extends Exception>
booleanreplaceAllIf
(Throwables.Predicate<? super K, X> predicate, E oldValue, E newValue) Replace the specifiedoldValue
(all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
with the specifiednewValue
.<X extends Exception>
booleanreplaceAllIf
(Throwables.Predicate<? super K, X> predicate, Collection<? extends E> oldValues, E newValue) Deprecated.<X extends Exception>
booleanReplace the specifiedoldValue
(one occurrence) from the value set associated with keys which satisfy the specifiedpredicate
with the specifiednewValue
.<X extends Exception>
booleanreplaceIf
(Throwables.Predicate<? super K, X> predicate, E oldValue, E newValue) Replace the specifiedoldValue
(one occurrence) from the value set associated with keys which satisfy the specifiedpredicate
with the specifiednewValue
.int
size()
stream()
toMap()
toMap
(IntFunction<? extends M> supplier) toString()
int
Returns the total count of all the elements in all values.unwrap()
Returns a view of this multimap as aMap
from each distinct key to the nonempty collection of that key's associated values.values()
-
Method Details
-
get
- Parameters:
key
-- Returns:
-
getOrDefault
Gets the or default.- Parameters:
key
-defaultValue
-- Returns:
-
put
- Parameters:
key
-e
-- Returns:
-
putIfAbsent
If the specified value is not already associated with the specified key associates it with the given key and returnstrue
, else returnsfalse
.- Parameters:
key
-e
-- Returns:
-
putIfKeyAbsent
If the specified key is not already associated with any value (or is mapped tonull
) associates it with the given value and returnstrue
, else returnsfalse
.- Parameters:
key
-e
-- Returns:
-
putAll
- Parameters:
key
-c
-- Returns:
-
putAllIfKeyAbsent
If the specified key is not already associated with any value (or is mapped tonull
) associates it with the given values in the specifiedcollection
and returnstrue
, else returnsfalse
.- Parameters:
key
-c
-- Returns:
-
putAll
- Parameters:
m
-- Returns:
-
putAll
- Parameters:
m
-- Returns:
-
remove
- Parameters:
key
-e
-- Returns:
-
removeAll
Removes the all.- Parameters:
key
-- Returns:
- values associated with specified key.
-
removeAll
Removes the all.- Parameters:
key
-c
-- Returns:
-
removeAll
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
Removes the all.- Parameters:
m
-- Returns:
-
removeIf
public <X extends Exception> boolean removeIf(E value, Throwables.Predicate<? super K, X> predicate) throws XRemove the specified value (one occurrence) from the value set associated with keys which satisfy the specifiedpredicate
.- Type Parameters:
X
-- Parameters:
value
-predicate
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
removeIf
public <X extends Exception> boolean removeIf(E value, Throwables.BiPredicate<? super K, ? super V, throws XX> predicate) Remove the specified value (one occurrence) from the value set associated with keys which satisfy the specifiedpredicate
.- Type Parameters:
X
-- Parameters:
value
-predicate
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
removeAllIf
public <X extends Exception> boolean removeAllIf(Collection<?> values, Throwables.Predicate<? super K, X> predicate) throws XRemove the specified values (all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
.- Type Parameters:
X
-- Parameters:
values
-predicate
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
removeAllIf
public <X extends Exception> boolean removeAllIf(Collection<?> values, Throwables.BiPredicate<? super K, ? super V, throws XX> predicate) Remove the specified values (all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
.- Type Parameters:
X
-- Parameters:
values
-predicate
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
removeAllIf
public <X extends Exception> boolean removeAllIf(Throwables.Predicate<? super K, X> predicate) throws XRemove all the values associated with keys which satisfy the specifiedpredicate
.- Type Parameters:
X
-- Parameters:
predicate
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
removeAllIf
public <X extends Exception> boolean removeAllIf(Throwables.BiPredicate<? super K, ? super V, throws XX> predicate) Remove all the values associated with keys which satisfy the specifiedpredicate
.- Type Parameters:
X
-- Parameters:
predicate
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
replace
Replace the specifiedoldValue
(one occurrence) with the specifiednewValue
.False
is returned if nooldValue
is found.- Parameters:
key
-oldValue
-newValue
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.
-
replaceAll
Replace the specifiedoldValue
(all occurrences) with the specifiednewValue
.False
is returned if nooldValue
is found.- Parameters:
key
-oldValues
-newValue
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.
-
replaceAll
Deprecated.Replace all the specifiedoldValues
(all occurrences) with single specifiednewValue
.False
is returned if nooldValue
is found.- Parameters:
key
-oldValues
-newValue
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.
-
replaceIf
public <X extends Exception> boolean replaceIf(Throwables.Predicate<? super K, X> predicate, E oldValue, E newValue) throws XReplace the specifiedoldValue
(one occurrence) from the value set associated with keys which satisfy the specifiedpredicate
with the specifiednewValue
.- Type Parameters:
X
-- Parameters:
predicate
-oldValue
-newValue
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
replaceIf
public <X extends Exception> boolean replaceIf(Throwables.BiPredicate<? super K, ? super V, throws XX> predicate, E oldValue, E newValue) Replace the specifiedoldValue
(one occurrence) from the value set associated with keys which satisfy the specifiedpredicate
with the specifiednewValue
.- Type Parameters:
X
-- Parameters:
predicate
-oldValue
-newValue
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
replaceAllIf
public <X extends Exception> boolean replaceAllIf(Throwables.Predicate<? super K, X> predicate, E oldValue, E newValue) throws XReplace the specifiedoldValue
(all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
with the specifiednewValue
.- Type Parameters:
X
-- Parameters:
predicate
-oldValue
-newValue
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
replaceAllIf
public <X extends Exception> boolean replaceAllIf(Throwables.BiPredicate<? super K, ? super V, throws XX> predicate, E oldValue, E newValue) Replace the specifiedoldValue
(all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
with the specifiednewValue
.- Type Parameters:
X
-- Parameters:
predicate
-oldValue
-newValue
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- 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 XDeprecated.Replace all the specifiedoldValue
(all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
with single specifiednewValue
.- Type Parameters:
X
-- Parameters:
predicate
-oldValues
-newValue
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
replaceAllIf
@Deprecated public <X extends Exception> boolean replaceAllIf(Throwables.BiPredicate<? super K, ? super V, throws XX> predicate, Collection<? extends E> oldValues, E newValue) Deprecated.Replace all the specifiedoldValue
(all occurrences) from the value set associated with keys which satisfy the specifiedpredicate
with single specifiednewValue
.- Type Parameters:
X
-- Parameters:
predicate
-oldValues
-newValue
-- Returns:
true
if this Multimap is modified by this operation, otherwisefalse
.- Throws:
X
- the x
-
replaceAll
public <X extends Exception> void replaceAll(Throwables.BiFunction<? super K, ? super V, throws X? extends V, X> function) The associated keys will be removed if null or empty values are returned by the specifiedfunction
.- Type Parameters:
X
-- Parameters:
function
-- Throws:
X
- the x
-
contains
- Parameters:
key
-e
-- Returns:
-
containsKey
- Parameters:
key
-- Returns:
-
containsValue
- Parameters:
e
-- Returns:
-
containsAll
- Parameters:
key
-c
-- Returns:
-
filterByKey
public <X extends Exception> Multimap<K,E, filterByKeyV> (Throwables.Predicate<? super K, X> filter) throws XFilter by key.- Type Parameters:
X
-- Parameters:
filter
-- Returns:
- Throws:
X
- the x
-
filterByValue
public <X extends Exception> Multimap<K,E, filterByValueV> (Throwables.Predicate<? super V, X> filter) throws XFilter by value.- Type Parameters:
X
-- Parameters:
filter
-- Returns:
- Throws:
X
- the x
-
filter
public <X extends Exception> Multimap<K,E, filterV> (Throwables.BiPredicate<? super K, ? super V, throws XX> filter) - Type Parameters:
X
-- Parameters:
filter
-- Returns:
- Throws:
X
- the x
-
forEach
public <X extends Exception> void forEach(Throwables.BiConsumer<? super K, ? super V, throws XX> action) - Type Parameters:
X
-- Parameters:
action
-- Throws:
X
- the x
-
flatForEach
public <X extends Exception> void flatForEach(Throwables.BiConsumer<? super K, ? super E, throws XX> action) Flat for each.- Type Parameters:
X
-- Parameters:
action
-- Throws:
X
- the x
-
forEachKey
For each key.- Type Parameters:
X
-- Parameters:
action
-- Throws:
X
- the x
-
forEachValue
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 XFlat 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, throws XX> mappingFunction) 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, throws X? extends V, X> remappingFunction) 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, throws X? extends V, X> remappingFunction) 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, throws X? extends V, X> remappingFunction) 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, throws X? extends V, X> remappingFunction) 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
-
inverse
public <VV extends Collection<K>,M extends Multimap<E, M inverseK, VV>> (IntFunction<? extends M> multimapSupplier) -
keySet
-
values
-
flatValues
-
flatValues
- Type Parameters:
R
-- Parameters:
supplier
-- Returns:
-
entrySet
-
toMap
-
toMap
- Type Parameters:
M
-- Parameters:
supplier
-- Returns:
-
toMultiset
-
unwrap
Returns a view of this multimap as aMap
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
-
entryStream
-
clear
public void clear()Clear. -
size
public int size() -
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, throws XV>, R, X> func) - 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, throws XV>, R, X> func) - Type Parameters:
R
-X
-- Parameters:
func
-- Returns:
- Throws:
X
- the x
-
accept
public <X extends Exception> void accept(Throwables.Consumer<? super Multimap<K, E, throws XV>, X> action) - Type Parameters:
X
-- Parameters:
action
-- Throws:
X
- the x
-
acceptIfNotEmpty
public <X extends Exception> If.OrElse acceptIfNotEmpty(Throwables.Consumer<? super Multimap<K, E, throws XV>, X> action) Accept if not empty.- Type Parameters:
X
-- Parameters:
action
-- Throws:
X
- the x
-
hashCode
public int hashCode() -
equals
-
toString
-