Top Description Methods
org.eclipse.collections.api.multimap

public Interface Multimap<K, V>

Known Direct Subinterfaces
org.eclipse.collections.api.multimap.MutableMultimap, org.eclipse.collections.api.multimap.bag.BagMultimap, org.eclipse.collections.api.multimap.ordered.OrderedIterableMultimap, org.eclipse.collections.api.multimap.set.SetMultimap, org.eclipse.collections.api.multimap.ImmutableMultimap
Annotations
@SuppressWarnings:JavaDoc
Type Parameters
<K>
the type of keys used
<V>
the type of mapped values
Imports
java.util.Collection, org.eclipse.collections.api.RichIterable, org.eclipse.collections.api.bag.Bag, org.eclipse.collections.api.block.function.Function, .Function0, .Function2, org.eclipse.collections.api.block.predicate.Predicate2, org.eclipse.collections.api.block.procedure.Procedure, .Procedure2, org.eclipse.collections.api.map.MutableMap, org.eclipse.collections.api.set.SetIterable, org.eclipse.collections.api.tuple.Pair

This collection is a type of Map that can associate multiple values for keys.

Unlike Map however, this interface is read-only so the results of access methods such as get(Object) return a view onto the values associated with that key. The MutableMultimap sub-interface provides methods to mutate the collection.

The advantages to using this container over a Map<K, Collection<V>> is that all of the handling of the value collection can be done automatically. It also allows implementations to further specialize in how duplicate values will be handled. Value collections with list semantics would allow duplicate values for a key, while those implementing set semantics would not. The value collections can never be empty.

Internal iteration methods for keys and values (singly - forEachKey(Procedure), forEachValue(Procedure), and together - forEachKeyValue(Procedure2)), forEachKeyMultiValues(Procedure2)) are provided to allow flexible browsing of the collection's contents. Similarly, views also are provided for keys (keysView()), values (valuesView()) and the combination thereof (keyValuePairsView(), keyMultiValuePairsView()).

Since
1.0

Method Summary

Modifier and TypeMethod and Description
public <K2, V2> Multimap<K2, V2>

Returns:

a new Multimap, which contains elements as a result of the transformation
collectKeyMultiValues
(Function<? super K, ? extends K2>
Function to use transformation to get the key
keyFunction
,
Function<? super V, ? extends V2>
Function to use transformation to get the values
valueFunction
)

Returns a new multimap with the results of applying the specified keyFunction and valueFunction on each key and corresponding values of the source multimap.

public <K2, V2, R extends MutableMultimap<K2, V2>> R

Returns:

target, which contains appended elements as a result of the transformation
collectKeyMultiValues
(Function<? super K, ? extends K2>
Function to use transformation to get the key
keyFunction
,
Function<? super V, ? extends V2>
Function to use transformation to get the values
valueFunction
,
R
the Multimap to append for all elements in this Multimap that are evaluated in keyFunction and valueFunction
target
)

Same as the collectKeyMultiValues method but uses the specified target multimap for the results.

public <K2, V2> Multimap<K2, V2>

Returns:

Multimap, which contains elements as a result of the transformation
collectKeysValues
(Function2<? super K, ? super V, Pair<K2, V2>>
a Function2 to use for transformation
function
)

Returns a new multimap with the results of applying the specified function on each key and value of the source multimap.

public <K2, V2, R extends MutableMultimap<K2, V2>> R

Returns:

target, which contains appended elements as a result of the transformation
collectKeysValues
(Function2<? super K, ? super V, Pair<K2, V2>>
a Function2 to use for transformation
function
,
R
the Multimap to append for all elements in this Multimap that are evaluated in function
target
)

Same as the collect method but uses the specified target multimap for the results.

public <V2> Multimap<K, V2>

Returns:

Multimap, which contains elements as a result of the transformation
collectValues
(Function<? super V, ? extends V2>
a Function to use for transformation
function
)

Returns a new multimap with the results of applying the specified function on each value of the source multimap.

public <V2, R extends MutableMultimap<K, V2>> R

Returns:

target, which contains appended elements as a result of the transformation
collectValues
(Function<? super V, ? extends V2>
a Function to use for transformation
function
,
R
the Multimap to append for all elements in this Multimap that are evaluated in function
target
)

Same as the collect method but uses the specified target multimap for the results.

public boolean
containsKey(Object
the key to search for
key
)

Returns true if any values are mapped to the specified key.

public boolean
containsKeyAndValue(Object
the key to search for
key
,
Object
the value to search for
value
)

Returns true if the specified key-value pair is mapped.

public boolean
containsValue(Object
the value to search for
value
)

Returns true if any key is mapped to the specified value.

public boolean
equals(Object obj)

Compares the specified object with this Multimap for equality.

public Multimap<V, K>
flip()

Given a Multimap from Domain -> Range return a multimap from Range -> Domain.

public void
forEachKey(Procedure<? super K> procedure)

Calls the procedure with each key.

public void
forEachKeyMultiValues(Procedure2<? super K, ? super RichIterable<V>> procedure)

Calls the procedure with each key-Iterable[value].

public void
forEachKeyValue(Procedure2<? super K, ? super V> procedure)

Calls the procedure with each key-value pair.

public void
forEachValue(Procedure<? super V> procedure)

Calls the procedure with each value.

public RichIterable<V>
get(K
the key to search for
key
)

Returns a view of all values associated with the given key.

public int
hashCode()

Returns the hash code for this Multimap.

public boolean
isEmpty()

Returns true if there are no entries.

public Bag<K>
keyBag()

Returns a Bag of keys with the count corresponding to the number of mapped values.

public RichIterable<Pair<K, RichIterable<V>>>
keyMultiValuePairsView()

Returns a lazy view of the pair of a key and a lazy view of the values mapped to that key.

public SetIterable<K>
keySet()

Returns an unmodifiable SetIterable of keys with O(1) complexity.

public RichIterable<K>
keysView()

Returns a lazy view of the unique keys.

public RichIterable<Pair<K, V>>
keyValuePairsView()

Returns a lazy view of all the key/value pairs.

public RichIterable<RichIterable<V>>
multiValuesView()

Returns an unmodifiable view of all the values mapped to each key.

public Multimap<K, V>
newEmpty()

Creates a new instance of the same implementation type, using the default capacity and growth parameters.

public boolean
notEmpty()

Returns true if there is at least one entry.

public Multimap<K, V>

Returns:

Multimap, which contains elements that don't satisfy the predicate
rejectKeysMultiValues
(Predicate2<? super K, ? super RichIterable<V>>
a Predicate2 to use as the reject criteria
predicate
)

Returns all elements of the source multimap that don't satisfy the predicate.

public <R extends MutableMultimap<K, V>> R

Returns:

target, which contains appended elements that don't satisfy the predicate
rejectKeysMultiValues
(Predicate2<? super K, ? super RichIterable<V>>
a Predicate2 to use as the reject criteria
predicate
,
R
the Multimap to append to for all elements in this Multimap that don't satisfy the predicate
target
)

Same as the reject method but uses the specified target multimap for the results.

public Multimap<K, V>

Returns:

Multimap, which contains elements that don't satisfy the predicate
rejectKeysValues
(Predicate2<? super K, ? super V>
a Predicate2 to use as the reject criteria
predicate
)

Returns all elements of the source multimap that don't satisfy the predicate.

public <R extends MutableMultimap<K, V>> R

Returns:

target, which contains appended elements that don't satisfy the predicate
rejectKeysValues
(Predicate2<? super K, ? super V>
a Predicate2 to use as the reject criteria
predicate
,
R
the Multimap to append to for all elements in this Multimap that don't satisfy the predicate
target
)

Same as the reject method but uses the specified target multimap for the results.

public Multimap<K, V>

Returns:

Multimap, which contains elements as a result of the select criteria
selectKeysMultiValues
(Predicate2<? super K, ? super RichIterable<V>>
a Predicate2 to use as the select criteria
predicate
)

Returns all elements of the source multimap that satisfies the predicate.

public <R extends MutableMultimap<K, V>> R

Returns:

target, which contains appended elements as a result of the select criteria
selectKeysMultiValues
(Predicate2<? super K, ? super RichIterable<V>>
a Predicate2 to use as the select criteria
predicate
,
R
the Multimap to append to for all elements in this Multimap that satisfy the predicate
target
)

Same as the select method but uses the specified target multimap for the results.

public Multimap<K, V>

Returns:

Multimap, which contains elements as a result of the select criteria
selectKeysValues
(Predicate2<? super K, ? super V>
a Predicate2 to use as the select criteria
predicate
)

Returns all elements of the source multimap that satisfies the predicate.

public <R extends MutableMultimap<K, V>> R

Returns:

target, which contains appended elements as a result of the select criteria
selectKeysValues
(Predicate2<? super K, ? super V>
a Predicate2 to use as the select criteria
predicate
,
R
the Multimap to append to for all elements in this Multimap that satisfy the predicate
target
)

Same as the select method but uses the specified target multimap for the results.

public int
size()

Returns the number of key-value entry pairs.

public int
sizeDistinct()

Returns the number of distinct keys.

public ImmutableMultimap<K, V>
toImmutable()

Returns an immutable copy of this Multimap if it is not already immutable.

public MutableMap<K, RichIterable<V>>
toMap()

Returns a new MutableMap of keys from this Multimap to the mapped values as a RichIterable.

public <R extends Collection<V>> MutableMap<K, R>
toMap(Function0<R>
used to create the collections that hold the values and affects the return type
collectionFactory
)

Returns a new MutableMap of keys from this Multimap to the mapped values as a RichIterable.

public MutableMultimap<K, V>
toMutable()

Returns a mutable copy of this Multimap.

public RichIterable<V>
valuesView()

Returns a lazy flattened view of all the values.

Method Detail

collectKeyMultiValuesback to summary
public <K2, V2> Multimap<K2, V2> collectKeyMultiValues(Function<? super K, ? extends K2> keyFunction, Function<? super V, ? extends V2> valueFunction)

Returns a new multimap with the results of applying the specified keyFunction and valueFunction on each key and corresponding values of the source multimap. This method is also commonly called transform or map.

e.g.
return multimap.collectKeyMultiValues(each -> each + 1, Person::getLastName);
Parameters
keyFunction:Function<? super K, ? extends K2>

Function to use transformation to get the key

valueFunction:Function<? super V, ? extends V2>

Function to use transformation to get the values

Returns:Multimap<K2, V2>

a new Multimap, which contains elements as a result of the transformation

Since
10.0
collectKeyMultiValuesback to summary
public <K2, V2, R extends MutableMultimap<K2, V2>> R collectKeyMultiValues(Function<? super K, ? extends K2> keyFunction, Function<? super V, ? extends V2> valueFunction, R target)

Same as the collectKeyMultiValues method but uses the specified target multimap for the results.

e.g.
return multimap.collectKeyMultiValues(each -> each + 1, Person::getLastName, HashBagMultimap.<Integer, String>newMultimap());
Parameters
keyFunction:Function<? super K, ? extends K2>

Function to use transformation to get the key

valueFunction:Function<? super V, ? extends V2>

Function to use transformation to get the values

target:R

the Multimap to append for all elements in this Multimap that are evaluated in keyFunction and valueFunction

Returns:R

target, which contains appended elements as a result of the transformation

Since
10.0
collectKeysValuesback to summary
public <K2, V2> Multimap<K2, V2> collectKeysValues(Function2<? super K, ? super V, Pair<K2, V2>> function)

Returns a new multimap with the results of applying the specified function on each key and value of the source multimap. This method is also commonly called transform or map.

e.g.
return multimap.collectKeysValues(new Function2<Integer, Person, Pair<String, String>>()
{
    public Pair<String, String> valueOf(Integer age, Person person)
    {
        return Tuples.pair(age.toString(), person.getLastName());
    }
});
Parameters
function:Function2<? super K, ? super V, Pair<K2, V2>>

a Function2 to use for transformation

Returns:Multimap<K2, V2>

Multimap, which contains elements as a result of the transformation

Since
6.0
collectKeysValuesback to summary
public <K2, V2, R extends MutableMultimap<K2, V2>> R collectKeysValues(Function2<? super K, ? super V, Pair<K2, V2>> function, R target)

Same as the collect method but uses the specified target multimap for the results.

e.g.
return multimap.collectKeysValues(new Function2<Integer, Person, Pair<String, String>>()
{
    public Pair<String, String> valueOf(Integer age, Person person)
    {
        return Tuples.pair(age.toString(), person.getLastName());
    }
}, HashBagMultimap.<String, String>newMultimap());
Parameters
function:Function2<? super K, ? super V, Pair<K2, V2>>

a Function2 to use for transformation

target:R

the Multimap to append for all elements in this Multimap that are evaluated in function

Returns:R

target, which contains appended elements as a result of the transformation

Since
6.0
collectValuesback to summary
public <V2> Multimap<K, V2> collectValues(Function<? super V, ? extends V2> function)

Returns a new multimap with the results of applying the specified function on each value of the source multimap. This method is also commonly called transform or map.

e.g.
return multimap.collectValues(new Function<Person, String>()
{
    public String valueOf(Person person)
    {
        return person.getLastName();
    }
});
Parameters
function:Function<? super V, ? extends V2>

a Function to use for transformation

Returns:Multimap<K, V2>

Multimap, which contains elements as a result of the transformation

Since
6.0
collectValuesback to summary
public <V2, R extends MutableMultimap<K, V2>> R collectValues(Function<? super V, ? extends V2> function, R target)

Same as the collect method but uses the specified target multimap for the results.

e.g.
return multimap.collectValues(new Function<Person, String>()
{
    public String valueOf(Person person)
    {
        return person.getLastName();
    }
}, FastListMultimap.<Integer, String>newMultimap());
Parameters
function:Function<? super V, ? extends V2>

a Function to use for transformation

target:R

the Multimap to append for all elements in this Multimap that are evaluated in function

Returns:R

target, which contains appended elements as a result of the transformation

Since
6.0
containsKeyback to summary
public boolean containsKey(Object key)

Returns true if any values are mapped to the specified key.

Parameters
key:Object

the key to search for

containsKeyAndValueback to summary
public boolean containsKeyAndValue(Object key, Object value)

Returns true if the specified key-value pair is mapped.

Parameters
key:Object

the key to search for

value:Object

the value to search for

containsValueback to summary
public boolean containsValue(Object value)

Returns true if any key is mapped to the specified value.

Parameters
value:Object

the value to search for

equalsback to summary
public boolean equals(Object obj)

Compares the specified object with this Multimap for equality.

Two Multimaps are equal when their map views (as returned by toMap) are also equal.

In general, two Multimaps with identical key-value mappings may or may not be equal, depending on the type of the collections holding the values. If the backing collections are Sets, then two instances with the same key-value mappings are equal, but if the backing collections are Lists, equality depends on the ordering of the values for each key.

Any two empty Multimaps are equal, because they both have empty toMap views.

Annotations
@Override
flipback to summary
public Multimap<V, K> flip()

Given a Multimap from Domain -> Range return a multimap from Range -> Domain.

Since
6.0
forEachKeyback to summary
public void forEachKey(Procedure<? super K> procedure)

Calls the procedure with each key.

Given a Multimap with the contents:

{"key1" : ["val1", "val2", "val2"], "key2" : ["val3"]}

The given procedure would be invoked with the parameters:

["key1", "key2"]

forEachKeyMultiValuesback to summary
public void forEachKeyMultiValues(Procedure2<? super K, ? super RichIterable<V>> procedure)

Calls the procedure with each key-Iterable[value].

Given a Multimap with the contents:

{"key1" : ["val1", "val2", "val2"], "key2" : ["val3"]}

The given procedure would be invoked with the parameters:

[["key1", {@link RichIterable["val1", "val2", "val2"]}], ["key2", {@link RichIterable["val3"]}]]

Since
6.0
forEachKeyValueback to summary
public void forEachKeyValue(Procedure2<? super K, ? super V> procedure)

Calls the procedure with each key-value pair.

Given a Multimap with the contents:

{"key1" : ["val1", "val2", "val2"], "key2" : ["val3"]}

The given procedure would be invoked with the parameters:

[["key1", "val1"], ["key1", "val2"], ["key1", "val2"], ["key2", "val3"]]

forEachValueback to summary
public void forEachValue(Procedure<? super V> procedure)

Calls the procedure with each value.

Given a Multimap with the contents:

{"key1" : ["val1", "val2", "val2"], "key2" : ["val3"]}

The given procedure would be invoked with the parameters:

["val1", "val2", "val2", "val3"]

getback to summary
public RichIterable<V> get(K key)

Returns a view of all values associated with the given key.

If the given key does not exist, an empty RichIterable is returned.

Parameters
key:K

the key to search for

hashCodeback to summary
public int hashCode()

Returns the hash code for this Multimap.

The hash code of a Multimap is defined as the hash code of the map view, as returned by toMap.

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Returns true if there are no entries.

keyBagback to summary
public Bag<K> keyBag()

Returns a Bag of keys with the count corresponding to the number of mapped values.

keyMultiValuePairsViewback to summary
public RichIterable<Pair<K, RichIterable<V>>> keyMultiValuePairsView()

Returns a lazy view of the pair of a key and a lazy view of the values mapped to that key.

keySetback to summary
public SetIterable<K> keySet()

Returns an unmodifiable SetIterable of keys with O(1) complexity.

keysViewback to summary
public RichIterable<K> keysView()

Returns a lazy view of the unique keys.

keyValuePairsViewback to summary
public RichIterable<Pair<K, V>> keyValuePairsView()

Returns a lazy view of all the key/value pairs.

multiValuesViewback to summary
public RichIterable<RichIterable<V>> multiValuesView()

Returns an unmodifiable view of all the values mapped to each key.

newEmptyback to summary
public Multimap<K, V> newEmpty()

Creates a new instance of the same implementation type, using the default capacity and growth parameters.

notEmptyback to summary
public boolean notEmpty()

Returns true if there is at least one entry.

rejectKeysMultiValuesback to summary
public Multimap<K, V> rejectKeysMultiValues(Predicate2<? super K, ? super RichIterable<V>> predicate)

Returns all elements of the source multimap that don't satisfy the predicate.

e.g.
return multimap.rejectKeysMultiValues(new Predicate2<Integer, Iterable<Person>>()
{
    public boolean accept(Integer age, Iterable<Person> values)
    {
        return (age >= 18)
                 && ((RichIterable<Person>)values.size() >= 2);
    }
});
Parameters
predicate:Predicate2<? super K, ? super RichIterable<V>>

a Predicate2 to use as the reject criteria

Returns:Multimap<K, V>

Multimap, which contains elements that don't satisfy the predicate

Since
6.0
rejectKeysMultiValuesback to summary
public <R extends MutableMultimap<K, V>> R rejectKeysMultiValues(Predicate2<? super K, ? super RichIterable<V>> predicate, R target)

Same as the reject method but uses the specified target multimap for the results.

e.g.
return multimap.rejectKeysMultiValues(new Predicate2<Integer, Iterable<Person>>()
{
    public boolean accept(Integer age, Iterable<Person> values)
    {
        return (age >= 18)
                 && ((RichIterable<Person>)values.size() >= 2);
    }
}, FastListMultimap.newMultimap());
Parameters
predicate:Predicate2<? super K, ? super RichIterable<V>>

a Predicate2 to use as the reject criteria

target:R

the Multimap to append to for all elements in this Multimap that don't satisfy the predicate

Returns:R

target, which contains appended elements that don't satisfy the predicate

Since
6.0
rejectKeysValuesback to summary
public Multimap<K, V> rejectKeysValues(Predicate2<? super K, ? super V> predicate)

Returns all elements of the source multimap that don't satisfy the predicate.

e.g.
return multimap.rejectKeysValues(new Predicate2<Integer, Person>()
{
    public boolean accept(Integer age, Person person)
    {
        return (age >= 18)
                 && (person.getAddress().getCity().equals("Metuchen"));
    }
});
Parameters
predicate:Predicate2<? super K, ? super V>

a Predicate2 to use as the reject criteria

Returns:Multimap<K, V>

Multimap, which contains elements that don't satisfy the predicate

Since
6.0
rejectKeysValuesback to summary
public <R extends MutableMultimap<K, V>> R rejectKeysValues(Predicate2<? super K, ? super V> predicate, R target)

Same as the reject method but uses the specified target multimap for the results.

e.g.
return multimap.rejectKeysValues(new Predicate2<Integer, Person>()
{
    public boolean accept(Integer age, Person person)
    {
        return (age >= 18)
                 && (person.getAddress().getCity().equals("Metuchen"));
    }
}, FastListMultimap.newMultimap());
Parameters
predicate:Predicate2<? super K, ? super V>

a Predicate2 to use as the reject criteria

target:R

the Multimap to append to for all elements in this Multimap that don't satisfy the predicate

Returns:R

target, which contains appended elements that don't satisfy the predicate

Since
6.0
selectKeysMultiValuesback to summary
public Multimap<K, V> selectKeysMultiValues(Predicate2<? super K, ? super RichIterable<V>> predicate)

Returns all elements of the source multimap that satisfies the predicate. This method is also commonly called filter.

e.g.
return multimap.selectKeysMultiValues(new Predicate2<Integer, Iterable<Person>>()
{
    public boolean accept(Integer age, Iterable<Person> values)
    {
        return (age >= 18)
                 && ((RichIterable<Person>)values.size() >= 2);
    }
});
Parameters
predicate:Predicate2<? super K, ? super RichIterable<V>>

a Predicate2 to use as the select criteria

Returns:Multimap<K, V>

Multimap, which contains elements as a result of the select criteria

Since
6.0
selectKeysMultiValuesback to summary
public <R extends MutableMultimap<K, V>> R selectKeysMultiValues(Predicate2<? super K, ? super RichIterable<V>> predicate, R target)

Same as the select method but uses the specified target multimap for the results.

e.g.
return multimap.selectKeysMultiValues(new Predicate2<Integer, Iterable<Person>>()
{
    public boolean accept(Integer age, Iterable<Person> values)
    {
        return (age >= 18)
                 && ((RichIterable<Person>)values.size() >= 2);
    }
}, FastListMultimap.newMultimap());
Parameters
predicate:Predicate2<? super K, ? super RichIterable<V>>

a Predicate2 to use as the select criteria

target:R

the Multimap to append to for all elements in this Multimap that satisfy the predicate

Returns:R

target, which contains appended elements as a result of the select criteria

Since
6.0
selectKeysValuesback to summary
public Multimap<K, V> selectKeysValues(Predicate2<? super K, ? super V> predicate)

Returns all elements of the source multimap that satisfies the predicate. This method is also commonly called filter.

e.g.
return multimap.selectKeysValues(new Predicate2<Integer, Person>()
{
    public boolean accept(Integer age, Person person)
    {
        return (age >= 18)
                 && (person.getAddress().getCity().equals("Metuchen"));
    }
});
Parameters
predicate:Predicate2<? super K, ? super V>

a Predicate2 to use as the select criteria

Returns:Multimap<K, V>

Multimap, which contains elements as a result of the select criteria

Since
6.0
selectKeysValuesback to summary
public <R extends MutableMultimap<K, V>> R selectKeysValues(Predicate2<? super K, ? super V> predicate, R target)

Same as the select method but uses the specified target multimap for the results.

e.g.
return multimap.selectKeysValues(new Predicate2<Integer, Person>()
{
    public boolean accept(Integer age, Person person)
    {
        return (age >= 18)
                 && (person.getAddress().getCity().equals("Metuchen"));
    }
}, FastListMultimap.newMultimap());
Parameters
predicate:Predicate2<? super K, ? super V>

a Predicate2 to use as the select criteria

target:R

the Multimap to append to for all elements in this Multimap that satisfy the predicate

Returns:R

target, which contains appended elements as a result of the select criteria

Since
6.0
sizeback to summary
public int size()

Returns the number of key-value entry pairs.

This method is implemented with O(1) (constant-time) performance.

sizeDistinctback to summary
public int sizeDistinct()

Returns the number of distinct keys.

toImmutableback to summary
public ImmutableMultimap<K, V> toImmutable()

Returns an immutable copy of this Multimap if it is not already immutable. If the Multimap is immutable, it will return itself.

The returned Multimap will be Serializable if this Multimap is Serializable.

toMapback to summary
public MutableMap<K, RichIterable<V>> toMap()

Returns a new MutableMap of keys from this Multimap to the mapped values as a RichIterable.

toMapback to summary
public <R extends Collection<V>> MutableMap<K, R> toMap(Function0<R> collectionFactory)

Returns a new MutableMap of keys from this Multimap to the mapped values as a RichIterable.

Parameters
collectionFactory:Function0<R>

used to create the collections that hold the values and affects the return type

toMutableback to summary
public MutableMultimap<K, V> toMutable()

Returns a mutable copy of this Multimap.

valuesViewback to summary
public RichIterable<V> valuesView()

Returns a lazy flattened view of all the values.