Package com.groiss.ds
Class MultiMap<K,V>
java.lang.Object
com.groiss.ds.MultiMap<K,V>
- All Implemented Interfaces:
Serializable
A MultiMap maps keys to objects, where one key can be mapped to a list of objects.
By default the
MultiMap
is not threadsafe
there are two constructors where you can specify the concurrency settings MultiMap(boolean)
, MultiMap(boolean, int, int)
.
If nothing is specified, the multimap has an default initial capacity of 16 and an default subVectorSize of 16- See Also:
-
Constructor Summary
ConstructorDescriptionMultiMap()
Constructs an empty, not threadsafe MultiMap with default initialcapacity (16) and default subVectorSize (16)MultiMap
(boolean threadSafe) Constructs an empty MultiMapMultiMap
(boolean threadSafe, int capacity, int initialVectorSize) Constructs an empty MultiMap with the given settingsMultiMap
(int initialSubSize) Constructs a MultiMap and specifies an initial size for the Vectors holding the values to the keys.MultiMap
(int capacity, int initialSubSize) Creates a new MultiMap with the defined capacity and Vector size. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all mappings from this mapboolean
containsKey
(Object key) Returnstrue
if this map contains a mapping for the specified key.boolean
containsValue
(Object value) Returnstrue
if this map maps one or more keys to the specified value.int
Returns number of values mapped to a given key.Enumeration<List<V>>
elements()
Returns the elements in the mapentrySet()
Returns a set view of the mappings contained in this map.enumeration
(Object key) Returns all values mapped to a given key, or nullReturns the values to which this map maps the specified key or null if there's no mappinggetEntries
(Object key) Deprecated.getEntryList
(Object key) returns a list containing all entries, mapped to the given key; if no value is mapped to this key, an empty list is returnedboolean
Finds out if the objectkey
has a mapping to the objectvalue
.boolean
isEmpty()
Returnstrue
if this map contains no key-value mappings.keys()
Deprecated.since 8.0; usekeySet()
insteadkeySet()
Returns a set view of the keys contained in this map.void
Put the mapping of a key and an object to the map.void
Copies all of the mappings from the specified map to this mapvoid
putAll
(K key, Collection<? extends V> values) Adds all the values to the given keyvoid
putIfAbsent
(K key, V value) Put the mapping of a key and an object to the map.Removes the mapping for this key from this map if it is presentboolean
Removes the value for a certain key.int
size()
toString()
Collection<List<V>>
values()
Returns a collection view of the values contained in this map.valueSet()
Return all values in this map as a single flattened set.
-
Constructor Details
-
MultiMap
public MultiMap()Constructs an empty, not threadsafe MultiMap with default initialcapacity (16) and default subVectorSize (16) -
MultiMap
public MultiMap(boolean threadSafe) Constructs an empty MultiMap- Parameters:
threadSafe
- whether the map is threadsafe or not
-
MultiMap
public MultiMap(boolean threadSafe, int capacity, int initialVectorSize) Constructs an empty MultiMap with the given settings- Parameters:
threadSafe
-capacity
- capacity of the mapinitialVectorSize
- capacity of the created collections to store the values
-
MultiMap
public MultiMap(int initialSubSize) Constructs a MultiMap and specifies an initial size for the Vectors holding the values to the keys. Every new Vector will be created with this initial size. If you use the other constructor, the initial size will have a default value of 16.- Parameters:
initialSubSize
- New Vectors will be created with this size.
-
MultiMap
public MultiMap(int capacity, int initialSubSize) Creates a new MultiMap with the defined capacity and Vector size.- Parameters:
capacity
- The Map will have this initial size. The load factor is the standard value of 0.75.initialSubSize
- New Vectors will be created with this size.
-
-
Method Details
-
clear
public void clear()Removes all mappings from this map -
containsKey
Returnstrue
if this map contains a mapping for the specified key. More formally, returnstrue
if and only if this map contains a mapping for a keyk
such that(key==null ? k==null : key.equals(k))
.- Parameters:
key
- key whose presence in this map is to be tested.- Returns:
true
if this map contains a mapping for the specified key.
-
containsValue
Returnstrue
if this map maps one or more keys to the specified value. More formally, returnstrue
if and only if this map contains at least one mapping to a valuev
such that(value==null ? v==null : value.equals(v))
.- Parameters:
value
- value whose presence in this map is to be tested.- Returns:
true
if this map maps one or more keys to the specified value.
-
count
Returns number of values mapped to a given key.- Parameters:
key
- the key object- Returns:
- the number of objects stored with this key.
-
elements
Returns the elements in the map- Returns:
- an enumeration containung vectors of elements mapped to the keys
-
enumeration
Returns all values mapped to a given key, or null- Parameters:
key
- the key object- Returns:
- the values enumeration
-
entrySet
Returns a set view of the mappings contained in this map. Each element in the returned set is aMap.Entry
. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.- Returns:
- a set view of the mappings contained in this map.
-
get
Returns the values to which this map maps the specified key or null if there's no mapping- Parameters:
key
- key whose associated value is to be returned.- Returns:
- the values to which this map maps the specified key, or
null
if the map contains no mapping for this key. - See Also:
-
getEntries
Deprecated.since 8.0; useget(Object)
instead- See Also:
-
getEntryList
returns a list containing all entries, mapped to the given key; if no value is mapped to this key, an empty list is returned- Parameters:
key
- the key- Returns:
- list of values mapped to the key, or an empty list
-
hasValue
Finds out if the objectkey
has a mapping to the objectvalue
.- Parameters:
key
- The key object.value
- The desired value object.- Returns:
true
if the key object contains a mapping to the given value object. In all other cases (also if the key doesn't exist at all),false
will be returned.
-
isEmpty
public boolean isEmpty()Returnstrue
if this map contains no key-value mappings.- Returns:
true
if this map contains no key-value mappings.
-
keys
Deprecated.since 8.0; usekeySet()
insteadReturn the keys- Returns:
- an Enumeration containing the keys.
-
keySet
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.- Returns:
- a set view of the keys contained in this map.
-
put
Put the mapping of a key and an object to the map. If a mapping (list l) with this key is already present we put the value to end l.- Parameters:
key
- a keyvalue
- the value object.
-
putIfAbsent
Put the mapping of a key and an object to the map. If the mapping (list l) with this key already contains the object, nothing is done.- Parameters:
key
- a keyvalue
- the value object.
-
putAll
Adds all the values to the given key- Parameters:
key
- the key to which the values should be addedvalues
- the values
-
putAll
Copies all of the mappings from the specified map to this map- Parameters:
t
- Mappings to be stored in this map.
-
remove
Removes the mapping for this key from this map if it is present- Parameters:
key
- key whose mapping is to be removed from the map.- Returns:
- previous value associated with specified key, or
null
if there was no mapping for key.
-
remove
Removes the value for a certain key.- Parameters:
key
- the key objectvalue
- the value for the key
-
size
public int size() -
toString
-
values
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.- Returns:
- a collection view of the values contained in this map.
-
valueSet
Return all values in this map as a single flattened set.- Returns:
- the set of values in this map (flattened)
-
get(Object)
instead