public class MultiValueMap<K,V>
extends java.util.AbstractMap<K,V>
implements java.lang.Cloneable
MultivalueMap implements a hash table that permits multiple values per key. It's very similar to the MultiValueMap class provided by the Jakarta Commons Collections API, except that this class uses Java 5 generics.
Any value placed into a MultivalueMap must implement java.lang.Comparable.
Modifier and Type | Class and Description |
---|---|
static interface |
MultiValueMap.ValuesCollectionAllocator<V>
Used to allocate a new Collection for the values associated
with a key.
|
Constructor and Description |
---|
MultiValueMap()
Constructs a new, empty map with a default capacity and load factor.
|
MultiValueMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity and
the default load factor.
|
MultiValueMap(int initialCapacity,
float loadFactor)
Constructs a new, empty map with the specified initial capacity and
the specified load factor.
|
MultiValueMap(int initialCapacity,
float loadFactor,
MultiValueMap.ValuesCollectionAllocator<V> valuesCollectionAllocator)
Constructs a new, empty map with the specified initial capacity and
the specified load factor.
|
MultiValueMap(MultiValueMap.ValuesCollectionAllocator<V> valuesCollectionAllocator)
Constructs a new, empty map with a default capacity and load factor.
|
MultiValueMap(MultiValueMap<K,V> otherMap)
Construct a new map from the contents of an existing map.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all mappings from this map.
|
java.lang.Object |
clone()
Returns a shallow copy of this MultivalueMap instance.
|
boolean |
containsKey(java.lang.Object key)
Returns true if this map contains at least one value for
the specified key.
|
boolean |
containsKeyValue(K key,
V value)
Returns true if this map contains the specified value for
the specified key.
|
boolean |
containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the
specified value.
|
java.util.Collection<V> |
delete(K key)
Removes all mappings for a key from this map, if present.
|
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
Returns an unmodifiable Set view of the mappings contained
in this map.
|
boolean |
equals(java.lang.Object o)
Compares the specified object with this map for equality.
|
V |
get(java.lang.Object key)
Synonym for
getFirstValueForKey(K) , required by the Map
interface. |
java.util.Collection<V> |
getCollection(K key)
Returns an unmodifiable Collection containing all values
associated with the the specified key.
|
V |
getFirstValueForKey(K key)
Returns the first value in the set of values associated with a
key.
|
java.util.Collection<V> |
getValuesForKey(K key)
Return an unmodifiable Collection of all the values for a
specific key.
|
int |
getValuesForKey(K key,
java.util.Collection<V> values)
Copy all the values for a specific key into a caller-supplied
Collection.
|
int |
hashCode()
Returns the hash code value for this map.
|
boolean |
isEmpty()
Determine whether the map is empty.
|
java.util.Set<K> |
keySet()
Returns a Set containing all the keys in this map.
|
V |
put(K key,
V value)
Associates the specified value with the specified key in this
map.
|
void |
putAll(K key,
java.util.Collection<V> values)
Assocates all the objects in a Collection with a key.
|
void |
putAll(java.util.Map<? extends K,? extends V> fromMap)
Copies all of the mappings from the specified Map to
this map.
|
void |
putAll(MultiValueMap<K,V> fromMap)
Copies all of the mappings from the specified
MultivalueMap to this map.
|
boolean |
remove(java.lang.Object key,
java.lang.Object value)
Removes a single value from the set of values associated with a
key.
|
int |
size()
Returns the number of key-value mappings in this map.
|
int |
totalValuesForKey(K key)
Gets the total number of values mapped to a specific key.
|
java.util.Collection<V> |
values()
Returns a collection view of the values contained in this map.
|
public MultiValueMap()
public MultiValueMap(MultiValueMap.ValuesCollectionAllocator<V> valuesCollectionAllocator)
valuesCollectionAllocator
- object to use to allocate collections
of values for a key.public MultiValueMap(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity of the MaploadFactor
- the load factorjava.lang.IllegalArgumentException
- if the initial capacity is negative,
or if the load factor is nonpostive.public MultiValueMap(int initialCapacity, float loadFactor, MultiValueMap.ValuesCollectionAllocator<V> valuesCollectionAllocator)
initialCapacity
- the initial capacityloadFactor
- the load factorvaluesCollectionAllocator
- object to use to allocate collections
of values for a key.java.lang.IllegalArgumentException
- if the initial capacity is negative,
or if the load factor is nonpostive.public MultiValueMap(int initialCapacity)
initialCapacity
- the initial capacityjava.lang.IllegalArgumentException
- if the initial capacity is negative,
or if the load factor is nonpostive.public MultiValueMap(MultiValueMap<K,V> otherMap)
otherMap
- the map to clonepublic void clear()
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map<K,V>
containsKey
in class java.util.AbstractMap<K,V>
key
- key whose presence in this map is to be testedjava.lang.ClassCastException
- if the key is of an inappropriate type for
this map.java.lang.NullPointerException
- if the key is null and this map
does not not permit null keys.totalValuesForKey(K)
public boolean containsValue(java.lang.Object value)
public boolean containsKeyValue(K key, V value)
key
- the keyvalue
- the valuepublic java.util.Set<java.util.Map.Entry<K,V>> entrySet()
public boolean equals(java.lang.Object o)
Compares the specified object with this map for equality. Returns true if the given object is also a MultivalueMap and the two maps represent the same mappings. maps t1 and t2 represent the same mappings if t1.entrySet().equals(t2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.
Warning:: Because this method must compare the actual values stored in the map, and the values in a file, this method can be quite slow.
public java.util.Collection<V> getCollection(K key)
Returns an unmodifiable Collection containing all values associated with the the specified key. Returns null if the map contains no mapping for this key.
key
- key whose associated collection of values is to be returned.java.lang.ClassCastException
- if the key is of an inappropriate type for
this map.java.lang.NullPointerException
- if the key is null and this map
does not not permit null keys.containsKey(java.lang.Object)
,
getFirstValueForKey(K)
public V get(java.lang.Object key)
getFirstValueForKey(K)
, required by the Map
interface.public V getFirstValueForKey(K key)
Returns the first value in the set of values associated with a key. This method is especially useful when you know that there is only a single value associated with the key. Note that "first" does not mean "first value ever associated with the key." Instead, it means "first value in the sorted list of values for the key."
key
- key whose associated value is to be returned.java.lang.NullPointerException
- if the key is null and this map
does not not permit null keys.containsKey(java.lang.Object)
,
get(java.lang.Object)
,
totalValuesForKey(K)
public int hashCode()
Returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry in the map's entrySet() view. This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any two maps t1 and t2, as required by the general contract of Object.hashCode.
hashCode
in interface java.util.Map<K,V>
hashCode
in class java.util.AbstractMap<K,V>
equals(Object)
public boolean isEmpty()
public java.util.Set<K> keySet()
The set is backed by the map, so changes to the map are reflected in the set. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. Neither the set nor its associated iterator supports any of the set-modification methods (e.g., add(), remove(), etc). If you attempt to call any of those methods, the called method will throw an UnsupportedOperationException.
public V put(K key, V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, this value is added to the list of values associated with the key. This map class does not permit a null value to be stored.
put
in interface java.util.Map<K,V>
put
in class java.util.AbstractMap<K,V>
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.java.lang.ClassCastException
- if the class of the specified key or
value prevents it from being stored
in this map.java.lang.IllegalArgumentException
- if some aspect of this key or value
prevents it from being stored in this
map.java.lang.NullPointerException
- the specified key or value is
null.public void putAll(java.util.Map<? extends K,? extends V> fromMap)
Copies all of the mappings from the specified Map to this map. These mappings will be added to any mappings that this map had for any of the keys currently in the specified map.
putAll
in interface java.util.Map<K,V>
putAll
in class java.util.AbstractMap<K,V>
fromMap
- Mappings to be stored in this map.java.lang.ClassCastException
- if the class of a key or value in the
specified map prevents it from being
stored in this map.java.lang.IllegalArgumentException
- some aspect of a key or value in the
specified map prevents it from being
stored in this map.java.lang.NullPointerException
- the specified key or value is
null.public void putAll(MultiValueMap<K,V> fromMap)
Copies all of the mappings from the specified MultivalueMap to this map. These mappings will be added to any mappings that this map had for any of the keys currently in the specified map.
fromMap
- Mappings to be stored in this map.java.lang.ClassCastException
- if the class of a key or value in the
specified map prevents it from being
stored in this map.java.lang.IllegalArgumentException
- some aspect of a key or value in the
specified map prevents it from being
stored in this map.java.lang.NullPointerException
- the specified key or value is
null.public void putAll(K key, java.util.Collection<V> values)
for (Iterator it = values.iterator(); it.hasNext(); ) map.put (key, it.next());
key
- the keyvalues
- the collection of values to associate with the keypublic java.util.Collection<V> delete(K key)
Removes all mappings for a key from this map, if present.
key
- key whose mappings are to be removed from the map.public boolean remove(java.lang.Object key, java.lang.Object value)
public int size()
Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
public int totalValuesForKey(K key)
key
- the key to testjava.lang.NullPointerException
- if the key is null and this map
does not not permit null keys.getValuesForKey(K)
public java.util.Collection<V> values()
Returns a collection view of the values contained in this map. The returned collection is a "thin" view of the values contained in this map. If a value is associated with more than one key (as determined by the value's equals() method), it will only appear once in the returned Collection. The values are sorted (via their compareTo() methods) in the returned Collection.
Warning: Unlike the SDK's Map class, the returned Collection is not backed by this map; instead, it represents a snapshot of the values in the map. Subsequent changes to this map object are not reflected in the returned Collection.
public java.util.Collection<V> getValuesForKey(K key)
key
- The keyjava.lang.NullPointerException
- if the key is null and this map
does not not permit null keys.keySet()
,
totalValuesForKey(K)
,
values()
public int getValuesForKey(K key, java.util.Collection<V> values)
key
- The keyvalues
- The Collection to receive the valuesjava.lang.NullPointerException
- if the key is null and this map
does not not permit null keys.keySet()
,
totalValuesForKey(K)
,
values()