Package com.tccc.kos.commons.util
Class MultiValueMap<K,V>
java.lang.Object
com.tccc.kos.commons.util.MultiValueMap<K,V>
Specialized class that behaves like a map of lists,
which means that each key can hold multiple values.
Usage
The constructor takes two generic parameters, K and V.- "K" is the type used as the map's key
- "V" is the data type stored in the value's list
new MultiValueMap<Integer, String>()
constructs a map whose key is an integer and whose value is a list of strings,
while:
new MultiValueMap<String, Object>()
constructs a map whose key is a string and whose value is a list of objects.
Example
public class MyClass {
private MultiValueMap<Integer, String> multiValueMap = new MultiValueMap<>();
multiValueMap.set("key1", "value1");
multiValueMap.add("key1", "value2");
multiValueMap.add("key1", "value3");
multiValueMap.remove("key1", "value2");
List<String> key1List = multiValueMap.get("key1");
assertThat(key1List, equalTo(List.of("value1", "value3"));
}
- Since:
- 1.0
- Version:
- 2022-10-26
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the given value to the list of items associated with the given key.void
Adds the contents of the given map into this object's internal map.void
addAll
(K key, Collection<V> values) Adds the contents of the given list to the list associated with the specified key.void
Add the specified key to the map with an empty list if the key doesn't already exist.void
clear()
Removes all items from this map.boolean
Return true if the specified value is contained in the list associated with the key.boolean
containsKey
(Object key) Determines if the map contains the given key or not.entrySet()
Returns a set of all map entries.Returns the list of values for the specified key.Returns the first value of the specified key.Returns the last value of the specified key.boolean
isEmpty()
Determines if the map is empty or not.keySet()
Returns a set of this map's key set.Removes the map entry for the specified key.void
Removes the specified value from the list associated with the given map.void
Sets the given key to a list containing one item, which is the given value.void
Copies the contents of the given map into this object's internal map.int
size()
Returns the number of items in the map.Collection<List<V>>
values()
Returns a collection of lists for all items in the map.
-
Constructor Details
-
MultiValueMap
public MultiValueMap()Constructs an empty multi-value map.
-
-
Method Details
-
size
public int size()Returns the number of items in the map.- Returns:
- number of items in the map
-
isEmpty
public boolean isEmpty()Determines if the map is empty or not.- Returns:
true
if this map is empty, otherwisefalse
-
addKey
Add the specified key to the map with an empty list if the key doesn't already exist.- Parameters:
key
- the key to add if needed
-
containsKey
Determines if the map contains the given key or not.- Parameters:
key
- the key to check for existence- Returns:
true
if this map contains an entry for the given key
-
contains
Return true if the specified value is contained in the list associated with the key.- Parameters:
key
- the key to checkval
- the value to check
-
getFirst
Returns the first value of the specified key.- Parameters:
key
- the key to return a value for- Returns:
- the first value for the key
-
getLast
Returns the last value of the specified key.- Parameters:
key
- the key to return a value for- Returns:
- the last value for the key
-
get
Returns the list of values for the specified key.- Parameters:
key
- the key to return the list for- Returns:
- a
List
of values, or empty list if there are none
-
set
Sets the given key to a list containing one item, which is the given value.- Parameters:
key
- the key for the mapvalue
- the value put in the associated list
-
setAll
Copies the contents of the given map into this object's internal map.- Parameters:
map
- the source map that gets copied
-
add
Adds the given value to the list of items associated with the given key.- Parameters:
key
- the key for the mapvalue
- the value to add to the associated list
-
addAll
Adds the contents of the given map into this object's internal map.- Parameters:
map
- the source map that gets added
-
addAll
Adds the contents of the given list to the list associated with the specified key.- Parameters:
key
- the key to add values tovalues
- the list to add
-
remove
Removes the map entry for the specified key.- Parameters:
key
- the key of the map- Returns:
- the list of items removed, or
null
if key did not exist
-
remove
Removes the specified value from the list associated with the given map.- Parameters:
key
- the key of the mapvalue
- the value in the associated list to remove
-
clear
public void clear()Removes all items from this map. -
keySet
Returns a set of this map's key set.- Returns:
- set of all keys
-
values
Returns a collection of lists for all items in the map.- Returns:
- collection of all lists
-
entrySet
Returns a set of all map entries.- Returns:
- set of all map entries
-