|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
An object that maps keys to multiple values. A multimap can contain duplicate keys, this is something that Map
objects cannot do.
The MultiMap
interface provides three collection views, which allow a multimap's contents to be viewed as a set
of keys, collection of values, or set of key-values mappings. The order of a multimap is defined as the order in which the
iterators on the multimap's collection views return their elements.
Note: great care must be exercised if mutable objects are used as multimap keys. The behavior of a multimap is not specified
if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the multimap. A
special case of this prohibition is that it is not permissible for a multimap to contain itself as a key. While it is
permissible for a multimap to contain itself as a value, extreme caution is advised: the equals
and
hashCode
methods are no longer well defined on a such a multimap.
All general-purpose multimap implementation classes should provide two "standard" constructors: a void (no arguments)
constructor which creates an empty multimap, and a constructor with a single argument of type MultiMap
, which
creates a new multimap with the same key-values mappings as its argument. In effect, the latter constructor allows the user to
copy any multimap, producing an equivalent multimap of the desired class. There is no way to enforce this recommendation
(as interfaces cannot contain constructors) but all of the general-purpose multimap implementations in the
com.neoworks.util
package comply.
Nested Class Summary | |
static interface |
MultiMap.Entry
A multimap entry (key-value pair). |
Method Summary | |
void |
clear()
Removes all mappings from this multimap (optional operation). |
boolean |
containsKey(java.lang.Object key)
Returns true if this multimap contains a mapping for the specified key. |
boolean |
containsValue(java.lang.Object value)
Returns true if this multimap maps one or more keys to the specified value. |
java.util.Set |
entrySet()
Returns a set view of the mappings contained in this multimap. |
boolean |
equals(java.lang.Object o)
Compares the specified object with this multimap for equality. |
java.util.Set |
get(java.lang.Object key)
Returns the set of values to which this multimap maps the specified key. |
int |
hashCode()
Returns the hash code value for this multimap. |
boolean |
isEmpty()
Returns true if this multimap contains no key-value mappings. |
java.util.Collection |
keys()
Returns a collection view of the keys contained in this multimap. |
java.util.Set |
put(java.lang.Object key,
java.lang.Object value)
Associates the specified value with the specified key in this multimap (optional operation). |
void |
putAll(MultiMap multimap)
Copies all of the mappings from the specified multimap to this multimap (optional operation). |
java.util.Set |
remove(java.lang.Object key)
Removes all the mappings for this key from this multimap if present (optional operation). |
java.lang.Object |
remove(java.lang.Object key,
java.lang.Object value)
Removes the (key, value) mapping from this multimap if present (optional operation). |
int |
size()
Returns the number of key-values mappings in this multimap. |
java.util.Collection |
uniqueKeys()
Returns a collection of the unique keys in this multimap. |
java.util.Collection |
values()
Returns a collection view of the values contained in this multimap. |
Method Detail |
public int size()
Integer.MAX_VALUE
elements, returns Integer.MAX_VALUE
.
public boolean isEmpty()
true
if this multimap contains no key-value mappings.
true
if this multimap contains no key-value mappings.public boolean containsKey(java.lang.Object key) throws java.lang.ClassCastException, java.lang.NullPointerException
true
if this multimap contains a mapping for the specified key.
key
- Key whose presence in this multimap is to be tested.
true
if this multimap contains a mapping for the specified key.
java.lang.ClassCastException
- If the key is of an inappropriate type for this multimap.
java.lang.NullPointerException
- If the key is null and this multimap does not not permit null keyspublic boolean containsValue(java.lang.Object value)
(value==null ? v==null : value.equals(v))
. This operation will probably require time linear in the
multimap size for most implementations of the MultiMap
interface.
value
- Value whose presence in this multimap is to be tested.
true
if this multimap maps one or more keys to the specified value.public java.util.Set get(java.lang.Object key) throws java.lang.ClassCastException, java.lang.NullPointerException
null
does indicate that the multimap contains no mappings for the
key; it's possible that the multimap explicitly maps the key to null, in this case the returned set will contain
null
possibly along with other objects. The containsKey
operation may also be used to
distinguish these two cases.
key
- Key whose associated value is to be returned.
Set
of values to which this multimap maps the specified key, or null
if the
multimap contains no mapping for this key.
java.lang.ClassCastException
- If the key is of an inappropriate type for this multimap.
java.lang.NullPointerException
- If the key is null
and this multimap does not not permit null
keyspublic java.util.Set put(java.lang.Object key, java.lang.Object value) throws java.lang.UnsupportedOperationException, java.lang.ClassCastException, java.lang.IllegalArgumentException, java.lang.NullPointerException
key
- Key with which the specified value is to be associated.value
- Value to be associated with the specified key.
Set
of values associated with the specified key.
java.lang.UnsupportedOperationException
- If the put operation is not supported by this multimap.
java.lang.ClassCastException
- If the class of the specified key or value prevents it from being stored in this
multimap.
java.lang.IllegalArgumentException
- If some aspect of this key or value prevents it from being stored in this multimap.
java.lang.NullPointerException
- This multimap does not permit null
keys or values, and the specified key
or value is null
.public java.util.Set remove(java.lang.Object key) throws java.lang.UnsupportedOperationException
key
- Key whose mappings is to be removed from the multimap.
Set
of values associated with the specified key, or null
if there were no mappings
for key.
java.lang.UnsupportedOperationException
- If the remove operation is not supported by this multimap.public java.lang.Object remove(java.lang.Object key, java.lang.Object value) throws java.lang.UnsupportedOperationException
value
- The value associated with the key to identify the item to removekey
- Key whose mapping to the value is to be removed from the multimap.
null
if the given mapping did not exist.
java.lang.UnsupportedOperationException
- If the remove operation is not supported by this multimap.public void putAll(MultiMap multimap) throws java.lang.UnsupportedOperationException, java.lang.ClassCastException, java.lang.IllegalArgumentException, java.lang.NullPointerException
multimap
- Mappings to be stored in this multimap.
java.lang.UnsupportedOperationException
- If the put operation is not supported by this multimap.
java.lang.ClassCastException
- If the class of the specified key or value prevents it from being stored in this
multimap.
java.lang.IllegalArgumentException
- If some aspect of a key or value in the specified multimap prevents it from being
stored in this multimap.
java.lang.NullPointerException
- This multimap does not permit null
keys or values, and the specified key
or value is null
.public void clear() throws java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException
- If clear is not supported by this multimap.public java.util.Collection keys()
Iterator.remove
,
Collection.remove
, removeAll
retainAll
, and clear
operations.
It does not support the add
or addAll
operations.
Note: As a MultiMap may contain duplicate keys this method may also return duplicate values.
public java.util.Collection uniqueKeys()
public java.util.Collection values()
Iterator.remove
,
Collection.remove
, removeAll
, retainAll
and clear
operations.
It does not support the add
or addAll
operations.
public java.util.Set entrySet()
MultiMap.Entry
. The set is backed by the multimap, so changes to the multimap are reflected in the set,
and vice-versa. If the multimap is modified while an iteration over the set is in progress, the results of the
iteration are undefined. The set supports element removal, which removes the corresponding mapping from the multimap,
via the Iterator.remove
, Set.remove
, removeAll
, retainAll
and
clear
operations. It does not support the add
or addAll
operations.
public boolean equals(java.lang.Object o)
t1.entrySet().equals(t2.entrySet())
. This ensures that the equals method works properly across different
implementations of the MultiMap
interface.
equals
in class java.lang.Object
o
- Object to be compared for equality with this multimap.
true
if the specified object is equal to this multimap.public int hashCode()
hashCodes
of each entry in the multimap's entrySet
view. This ensures that
t1.equals(t2)
implies that t1.hashCode()==t2.hashCode()
for any two multimaps t1 and t2,
as required by the general contract of Object.hashCode
.
hashCode
in class java.lang.Object
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |