Class MultiMap<K,V>

java.lang.Object
org.jvnet.hk2.component.MultiMap<K,V>
Type Parameters:
K - The key of the multi-map
V - The types in the multi-map
All Implemented Interfaces:
Serializable, Cloneable

public class MultiMap<K,V> extends Object implements Serializable, Cloneable
Map from a key to multiple values. Order is significant among values, and null values are allowed, although null keys are not.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final String
     
    private static final long
    For serialization
    private final Map<K,List<V>>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty multi-map with default concurrency controls
    Copy constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    private final List<V>
    _get(K k)
    Package private (for getting the raw map for direct manipulation by the habitat)
    final void
    add(K k, V v)
    Adds one more key-value pair.
    Creates a copy of the map that contains the exact same key and value set.
    boolean
    contains(K k1, V k2)
    Checks if the map contains the given key(s), also extending the search to including the sub collection.
    boolean
    Checks if the map contains the given key.
    Lists up all entries.
    boolean
    equals(Object another)
     
    final List<V>
    get(K k)
    Returns the elements indexed by the provided key
    private V
     
    getOne(K k)
    Gets the first value if any, or null.
    int
     
    Returns the set of keys associated with this MultiMap
    void
    mergeAll(MultiMap<K,V> another)
    This method merges all of the keys and values from another MultiMap into this MultiMap.
    private List<V>
    newList(Collection<? extends V> initialVals)
    Creates an optionally populated list to be used as an entry in the map.
    remove(K key)
    Removes an key value from the map
    boolean
    remove(K key, V entry)
    Removes an key value pair from the map.
    void
    set(K k, Collection<? extends V> v)
    Replaces all the existing values associated with the key by the given value.
    void
    set(K k, V v)
    Replaces all the existing values associated with the key by the given single value.
    int
    Returns the size of the map.
     
     

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For serialization
      See Also:
    • store

      private final Map<K,List<V>> store
    • NEWLINE

      private static final String NEWLINE
  • Constructor Details

    • MultiMap

      public MultiMap()
      Creates an empty multi-map with default concurrency controls
    • MultiMap

      public MultiMap(MultiMap<K,V> base)
      Copy constructor.
      Parameters:
      base - map to copy
  • Method Details

    • newList

      private List<V> newList(Collection<? extends V> initialVals)
      Creates an optionally populated list to be used as an entry in the map.
      Parameters:
      initialVal -
      Returns:
    • keySet

      public Set<K> keySet()
      Returns the set of keys associated with this MultiMap
      Returns:
      The set of keys currently available in this MultiMap. Will not return null, but may return a Set of lenght zero
    • add

      public final void add(K k, V v)
      Adds one more key-value pair.
      Parameters:
      k - key to store the entry under
      v - value to store in the k's values.
    • set

      public void set(K k, Collection<? extends V> v)
      Replaces all the existing values associated with the key by the given value. If v is empty the key k will be removed from the MultiMap.
      Parameters:
      k - key for the values
      v - Can be null or empty.
    • set

      public void set(K k, V v)
      Replaces all the existing values associated with the key by the given single value.
      Parameters:
      k - key for the values
      v - singleton value for k key

      This is short for set(k,Collections.singleton(v))

    • get

      public final List<V> get(K k)
      Returns the elements indexed by the provided key
      Parameters:
      k - key for the values
      Returns:
      Can be empty but never null. Read-only.
    • mergeAll

      public void mergeAll(MultiMap<K,V> another)
      This method merges all of the keys and values from another MultiMap into this MultiMap. If a key/value pair is found in both MultiMaps it is not re-added to this MultiMap, but is instead discarded
      Parameters:
      another - The MultiMap from which to add values to this MultiMap. If null this method does nothing
    • _get

      private final List<V> _get(K k)
      Package private (for getting the raw map for direct manipulation by the habitat)
      Parameters:
      k - the key
      Returns:
    • containsKey

      public boolean containsKey(K k)
      Checks if the map contains the given key.
      Parameters:
      k - key to test
      Returns:
      true if the map contains at least one element for this key
    • contains

      public boolean contains(K k1, V k2)
      Checks if the map contains the given key(s), also extending the search to including the sub collection.
      Parameters:
      k1 - key from top collection
      k2 - key (value) from inner collection
      Returns:
      true if the map contains at least one element for these keys
    • remove

      public List<V> remove(K key)
      Removes an key value from the map
      Parameters:
      key - key to be removed
      Returns:
      the value stored under this key or null if there was none
    • remove

      public boolean remove(K key, V entry)
      Removes an key value pair from the map. If the list of entries for that key is empty after the remove it will be removed from the set of keys
      Parameters:
      key - key to be removed
      entry - the entry to be removed from the key'ed list
      Returns:
      true if there was none that was deleted
    • getOne

      public V getOne(K k)
      Gets the first value if any, or null.

      This is useful when you know the given key only has one value and you'd like to get to that value.

      Parameters:
      k - key for the values
      Returns:
      null if the key has no values or it has a value but the value is null.
    • getFirst

      private V getFirst(K k)
    • entrySet

      public Set<Map.Entry<K,List<V>>> entrySet()
      Lists up all entries.
      Returns:
      a Set of Map.Entry of entries
    • toCommaSeparatedString

      public String toCommaSeparatedString()
      Returns:
      the map as "key=value1,key=value2,...."
    • clone

      public MultiMap<K,V> clone() throws CloneNotSupportedException
      Creates a copy of the map that contains the exact same key and value set. Keys and values won't cloned.
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • size

      public int size()
      Returns the size of the map. This returns the numbers of keys in the map, not the number of values
      Returns:
      integer or 0 if the map is empty
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object another)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object