Class MultiHashMap<K,V>
java.lang.Object
net.okapia.osid.torrefacto.collect.MultiHashMap<K,V>
- Type Parameters:
K- the key typeV- the value type
- All Implemented Interfaces:
MultiMap<K,V>
- Direct Known Subclasses:
IdMultiHashMap, TypeMultiHashMap
A
MultiMap that maps each key to a set of values, backed by
a LinkedHashMap of keys to LinkedHashSets. Both the
keys and the values under a key keep their insertion order.
The value collections are sets, so a key cannot map to the same
value twice; a repeated put of an existing key/value pair is
a no-op. A key is dropped once its last value is removed, so containsKey(K) returns false for it thereafter. Null keys
and null values are rejected.
size() reports the total number of key/value pairs (a value
shared by two keys counts twice), while values() returns the
distinct values across all keys. The collections returned by get(K), keySet(), values(), and asMap() are
unmodifiable.
This implementation is not synchronized.-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an emptyMultiHashMapwith the default initial capacity (16) and the default load factor (0.75).MultiHashMap(int initialCapacity) Constructs an emptyMultiHashMapwith the specified initial capacity and the default load factor (0.75).MultiHashMap(int initialCapacity, float loadFactor) Constructs an emptyMultiHashMapwith the specified initial capacity and load factor. -
Method Summary
Modifier and TypeMethodDescriptionMap<K, Collection<V>> asMap()Gets a Map of this MultiMap.voidclear()Removes all of the mappings from this map.booleancontainsKey(K key) Tests if this map contains a mapping for the specified key.booleancontainsKeyValue(K key, V value) Tests the map contains the key and value specified.booleancontainsValue(V value) Tests the map contains the value specified.Returns the values to which the specified key is mapped.booleanisEmpty()Tests if this map contains no key-value mappings.booleanTests if this map can be modified.keySet()Returns an immutable Collection view of the keys contained in this map.voidAssociates the specified value with the specified key in this map.voidputAll(Map<K, Collection<? extends V>> map) Stores a map of keys and values inside this map.voidputAll(K key, Collection<? extends V> values) Adds a collection of values to to a key in this map.voidStores a Multimap inside this map.voidRemoves the mapping for a key and all its related values.voidRemoves a specific key/value pair from the map.voidremoveValue(V value) Removes a value from all keys.longsize()Returns the number of key-value mappings in this map.values()Returns a Collection view of the values contained in this map.
-
Constructor Details
-
MultiHashMap
public MultiHashMap(int initialCapacity, float loadFactor) Constructs an emptyMultiHashMapwith the specified initial capacity and load factor.- Parameters:
initialCapacity- the initial capacityloadFactor- the load factor- Throws:
IllegalArgumentException- if the initial capacity is negative or the load factor is nonpositive
-
MultiHashMap
public MultiHashMap(int initialCapacity) Constructs an emptyMultiHashMapwith the specified initial capacity and the default load factor (0.75).- Parameters:
initialCapacity- the initial capacity.- Throws:
IllegalArgumentException- if the initial capacity is negative.
-
MultiHashMap
public MultiHashMap()Constructs an emptyMultiHashMapwith the default initial capacity (16) and the default load factor (0.75).
-
-
Method Details
-
isModifiable
public boolean isModifiable()Tests if this map can be modified.- Specified by:
isModifiablein interfaceMultiMap<K,V> - Returns:
trueif the map can be modified,falseif read-only
-
size
-
isEmpty
-
containsKey
Tests if this map contains a mapping for the specified key.- Specified by:
containsKeyin interfaceMultiMap<K,V> - Parameters:
key- the key whose presence in this map is to be tested- Returns:
trueif this map contains a mapping for the specified key,falseotherwise- Throws:
NullArgumentException-keyisnull
-
containsValue
Tests the map contains the value specified.- Specified by:
containsValuein interfaceMultiMap<K,V> - Parameters:
value- the value to search for- Returns:
trueif the map contains the value,falseotherwise- Throws:
NullArgumentException-valueisnull
-
containsKeyValue
Tests the map contains the key and value specified.- Specified by:
containsKeyValuein interfaceMultiMap<K,V> - Parameters:
key- the keyvalue- the value- Returns:
trueif the map contains the key/value pair,falseotherwise- Throws:
NullArgumentException-keyorvalueisnull
-
get
Returns the values to which the specified key is mapped. If the key is not defined, this method returns an empty list.- Specified by:
getin interfaceMultiMap<K,V> - Parameters:
key- the key- Returns:
- a collection of the values for the given key or an empty collection if the key is not found
- Throws:
NullArgumentException-keyisnull
-
put
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.- Specified by:
putin interfaceMultiMap<K,V> - Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- Throws:
NullArgumentException-keyorvalueisnull
-
putAll
Adds a collection of values to to a key in this map.- Specified by:
putAllin interfaceMultiMap<K,V> - Parameters:
key- the keyvalues- the values- Throws:
NullArgumentException-keyorvaluesisnull
-
putAll
Stores a map of keys and values inside this map.- Specified by:
putAllin interfaceMultiMap<K,V> - Parameters:
map- the map to store- Throws:
NullArgumentException-mapisnull
-
putAll
-
remove
-
remove
Removes a specific key/value pair from the map. The item is removed from the collection mapped to the specified key. Other values attached to that key are unaffected.- Specified by:
removein interfaceMultiMap<K,V> - Parameters:
key- the keyvalue- the value to remove- Throws:
NullArgumentException-keyorvalueisnull
-
removeValue
Removes a value from all keys.- Specified by:
removeValuein interfaceMultiMap<K,V> - Parameters:
value- to be removed from the map- Throws:
NullArgumentException-valueisnull
-
clear
-
keySet
-
values
-
asMap
-