Package com.pnfsoftware.jeb.util.collect
Class MultiMap<K,V>
java.lang.Object
com.pnfsoftware.jeb.util.collect.MultiMap<K,V>
- Type Parameters:
K- key typeV- type of value to be stored
A map of keys to lists, whose insertion order for the values of a given key is maintained. This
class is
serializable. Null key is allowed except for natural ordering; null
values are allowed.
Thread-safety: none.
Note: this class does not implement the Map interface, but attempts to follow its
guidelines and contracts the best it can. One notable exception is that this class does not
override hashCode/equals.
-
Constructor Summary
ConstructorsConstructorDescriptionMultiMap()Create a new multi-map.MultiMap(CollectionOrder keyOrder) Create a new multi-map. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Remove all key-value mappings.booleancontainsKey(K key) Determine whether this multi-map contains a key.booleancontainsValue(V value) Determine whether this multi-map contains a value.booleanCreate an explicit entry for a given key.Retrieve the list of values associated with the provided key.Retrieve the list of values associated with the provided key.Retrieve the list of values associated with the provided key.booleanisEmpty()Determine whether this multi-map has no values.keySet()Retrieve a read-only view of the keys.intkeySize()Get the number of keys in this multi-map.intAdd a key-value mapping.voidAdd all mappings from a map of keys to lists of values.voidputMulti(K key, Collection<? extends V> values) Add multiple elements mapped by the same key.Remove all values associated with a key.booleanremoveAll(Collection<K> keys) Remove all entries mapping to the collection of keys.intremoveMulti(K key, Collection<? extends V> values) Remove multiple values associated with a key.intremoveMulti(K key, Collection<? extends V> values, boolean removeEmptyBucket) Remove multiple values associated with a key.removeValue(K key, V value) Remove a value associated with a key.removeValue(K key, V value, boolean removeEmptyBucket) Remove a value associated with a key.intsize()Get the number of key-value mappings (per Map interface).toString()values()Retrieve all values.
-
Constructor Details
-
MultiMap
public MultiMap()Create a new multi-map. -
MultiMap
Create a new multi-map.- Parameters:
keyOrder- key ordering
-
-
Method Details
-
clear
public void clear()Remove all key-value mappings. -
keySize
public int keySize()Get the number of keys in this multi-map.- Returns:
- number of keys, including keys mapped to empty value lists
-
isEmpty
public boolean isEmpty()Determine whether this multi-map has no values.- Returns:
- true if no values are mapped
-
size
public int size()Get the number of key-value mappings (per Map interface). That is, the number of values.- Returns:
- number of values
-
put
Add a key-value mapping.- Parameters:
key- keyvalue- value- Returns:
- updated count of values associated with the key
-
putMulti
Add multiple elements mapped by the same key. Note that if an empty collection is provided, the entry for the provided key will not be be explicitly created.- Parameters:
key- keyvalues- values to add
-
putAll
Add all mappings from a map of keys to lists of values.- Parameters:
m- map of values to add
-
createKey
Create an explicit entry for a given key. If no values mapped by the given key previously existed, the associated value will be an empty collection.- Parameters:
key- key- Returns:
- true if the map did not already contain the key; false otherwise (the key entry existed before)
-
containsKey
Determine whether this multi-map contains a key.- Parameters:
key- key- Returns:
- true if the key is present
-
containsValue
Determine whether this multi-map contains a value.- Parameters:
value- value- Returns:
- true if any key maps to the value
-
get
Retrieve the list of values associated with the provided key.- Parameters:
key- a keysafe- if true, an empty list of values is returned if the key does not exist- Returns:
- a list, possibly null or empty (depending on the
safeparameter}
-
get
Retrieve the list of values associated with the provided key. Same asget(key, safe=false).- Parameters:
key- a key- Returns:
- a list, possibly null if nothing was
putfor the provided key
-
getSafe
Retrieve the list of values associated with the provided key. Same asget(key, safe=true).- Parameters:
key- key- Returns:
- a non-null list of values
-
remove
Remove all values associated with a key.- Parameters:
key- key- Returns:
- removed values, or null if the key was absent
-
removeValue
Remove a value associated with a key.- Parameters:
key- keyvalue- value to remove- Returns:
- removed value, or null if no value was removed
-
removeValue
Remove a value associated with a key.- Parameters:
key- keyvalue- value to removeremoveEmptyBucket- true to remove the key if its value list becomes empty- Returns:
- removed value, or null if no value was removed
-
removeMulti
Remove multiple values associated with a key.- Parameters:
key- keyvalues- values to remove- Returns:
- number of removed values
-
removeMulti
Remove multiple values associated with a key.- Parameters:
key- keyvalues- values to removeremoveEmptyBucket- true to remove the key if its value list becomes empty- Returns:
- number of removed values
-
values
Retrieve all values.- Returns:
- read-only collection of values
-
keySet
Retrieve a read-only view of the keys.WARNING: This method does not respect the
Map.keySet()contract: the set returned is read-only.- Returns:
- read-only view of the key set
-
removeAll
Remove all entries mapping to the collection of keys.Similar semantics as
Set.removeAll(Collection).- Parameters:
keys- keys to remove- Returns:
- true if the multi-map was modified
-
toString
-