Class MultiValueHashMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,List<V>>
io.github.connellite.collections.MultiValueHashMap<K,V>
Type Parameters:
K - the key type
V - the value element type
All Implemented Interfaces:
MultiValueMap<K,V>, Serializable, Cloneable, Map<K,List<V>>

public class MultiValueHashMap<K,V> extends HashMap<K,List<V>> implements MultiValueMap<K,V>, Serializable, Cloneable
Simple implementation of MultiValueMap that wraps a HashMap, storing multiple values in an ArrayList.

This Map implementation is generally not thread-safe. It is primarily designed for data structures exposed from request objects, for use in a single thread only.

See Also:
  • Constructor Details

    • MultiValueHashMap

      public MultiValueHashMap()
      Create a new MultiValueHashMap that wraps a HashMap.
    • MultiValueHashMap

      public MultiValueHashMap(int expectedSize)
      Create a new MultiValueHashMap that wraps a HashMap with the given initial capacity.
      Parameters:
      expectedSize - the expected number of entries
    • MultiValueHashMap

      public MultiValueHashMap(Map<K,List<V>> otherMap)
      Copy constructor: Create a new MultiValueHashMap with the same mappings as the specified Map.
      Parameters:
      otherMap - the Map whose mappings are to be placed in this Map
    • MultiValueHashMap

      public MultiValueHashMap(MultiValueMap<K,V> otherMap)
      Copy constructor: Create a new MultiValueHashMap with the same mappings as the specified MultiValueMap.
      Parameters:
      otherMap - the Map whose mappings are to be placed in this Map
  • Method Details

    • getFirst

      public V getFirst(K key)
      Description copied from interface: MultiValueMap
      Return the first value for the given key.
      Specified by:
      getFirst in interface MultiValueMap<K,V>
      Parameters:
      key - the key
      Returns:
      the first value for the specified key, or null
    • add

      public void add(K key, V value)
      Description copied from interface: MultiValueMap
      Add the given single value to the current list of values for the given key.
      Specified by:
      add in interface MultiValueMap<K,V>
      Parameters:
      key - the key
      value - the value to be added
    • addAll

      public void addAll(K key, List<? extends V> values)
      Description copied from interface: MultiValueMap
      Add all the values of the given list to the current list of values for the given key.
      Specified by:
      addAll in interface MultiValueMap<K,V>
      Parameters:
      key - they key
      values - the values to be added
    • addAll

      public void addAll(MultiValueMap<K,V> values)
      Description copied from interface: MultiValueMap
      Add all the values of the given MultiValueMap to the current values.
      Specified by:
      addAll in interface MultiValueMap<K,V>
      Parameters:
      values - the values to be added
    • set

      public void set(K key, V value)
      Description copied from interface: MultiValueMap
      Set the given single value under the given key.
      Specified by:
      set in interface MultiValueMap<K,V>
      Parameters:
      key - the key
      value - the value to set
    • setAll

      public void setAll(Map<K,V> values)
      Description copied from interface: MultiValueMap
      Set the given values under their associated key.
      Specified by:
      setAll in interface MultiValueMap<K,V>
      Parameters:
      values - the values
    • toSingleValueMap

      public Map<K,V> toSingleValueMap()
      Description copied from interface: MultiValueMap
      Return a Map with the first values contained in this MultiValueMap.
      Specified by:
      toSingleValueMap in interface MultiValueMap<K,V>
      Returns:
      a single value representation of this map
    • toUnmodifiableMap

      public Map<K,List<V>> toUnmodifiableMap()
      Description copied from interface: MultiValueMap
      Return an unmodifiable view of this map with immutable value lists. The returned map and its list values cannot be modified; changes to this map are not reflected in the returned instance.
      Specified by:
      toUnmodifiableMap in interface MultiValueMap<K,V>
      Returns:
      unmodifiable map with copied value lists
    • deepCopy

      public MultiValueHashMap<K,V> deepCopy()
      Create a deep copy of this Map.
      Returns:
      a copy of this Map, including a copy of each value-holding List entry
    • clone

      public MultiValueHashMap<K,V> clone()
      Overrides:
      clone in class HashMap<K,List<V>>