Class MultiValueMap<K,V>

java.lang.Object
com.tccc.kos.commons.util.MultiValueMap<K,V>

public class MultiValueMap<K,V> extends Object
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
For example:
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
    Constructor
    Description
    Constructs an empty multi-value map.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(K key, V value)
    Adds the given value to the list of items associated with the given key.
    void
    addAll(Map<K,V> map)
    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
    addKey(K key)
    Add the specified key to the map with an empty list if the key doesn't already exist.
    void
    Removes all items from this map.
    boolean
    contains(Object key, Object val)
    Return true if the specified value is contained in the list associated with the key.
    boolean
    Determines if the map contains the given key or not.
    Returns a set of all map entries.
    get(Object key)
    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
    Determines if the map is empty or not.
    Returns a set of this map's key set.
    Removes the map entry for the specified key.
    void
    remove(Object key, V value)
    Removes the specified value from the list associated with the given map.
    void
    set(K key, V value)
    Sets the given key to a list containing one item, which is the given value.
    void
    setAll(Map<K,V> map)
    Copies the contents of the given map into this object's internal map.
    int
    Returns the number of items in the map.
    Returns a collection of lists for all items in the map.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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, otherwise false
    • addKey

      public void addKey(K key)
      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

      public boolean containsKey(Object key)
      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

      public boolean contains(Object key, Object val)
      Return true if the specified value is contained in the list associated with the key.
      Parameters:
      key - the key to check
      val - the value to check
    • getFirst

      public V getFirst(Object key)
      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

      public V getLast(Object key)
      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

      public List<V> get(Object key)
      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

      public void set(K key, V value)
      Sets the given key to a list containing one item, which is the given value.
      Parameters:
      key - the key for the map
      value - the value put in the associated list
    • setAll

      public void setAll(Map<K,V> map)
      Copies the contents of the given map into this object's internal map.
      Parameters:
      map - the source map that gets copied
    • add

      public void add(K key, V value)
      Adds the given value to the list of items associated with the given key.
      Parameters:
      key - the key for the map
      value - the value to add to the associated list
    • addAll

      public void addAll(Map<K,V> map)
      Adds the contents of the given map into this object's internal map.
      Parameters:
      map - the source map that gets added
    • addAll

      public void addAll(K key, Collection<V> values)
      Adds the contents of the given list to the list associated with the specified key.
      Parameters:
      key - the key to add values to
      values - the list to add
    • remove

      public List<V> remove(Object key)
      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

      public void remove(Object key, V value)
      Removes the specified value from the list associated with the given map.
      Parameters:
      key - the key of the map
      value - the value in the associated list to remove
    • clear

      public void clear()
      Removes all items from this map.
    • keySet

      public Set<K> keySet()
      Returns a set of this map's key set.
      Returns:
      set of all keys
    • values

      public Collection<List<V>> values()
      Returns a collection of lists for all items in the map.
      Returns:
      collection of all lists
    • entrySet

      public Set<Map.Entry<K,List<V>>> entrySet()
      Returns a set of all map entries.
      Returns:
      set of all map entries