Class MultiMap<K,V>

java.lang.Object
org.faktorips.util.MultiMap<K,V>
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
SortedMultiMap

public class MultiMap<K,V> extends Object implements Serializable
A data structure that maps a key to a collection of values. Adding a value to the map is like putting a value to the collection regarding the specified key. If a value is requested by using the key a collection is returned.

Different kinds of multi maps can be created using different collection factories, or by calling the creator methods createWithListsAsValues() and createWithSetsAsValues(). The map then contains only lists or only sets respectively, depending on the use case.

See Also:
  • Constructor Details

  • Method Details

    • createWithSetsAsValues

      public static <K, V> MultiMap<K,V> createWithSetsAsValues()
    • createWithListsAsValues

      public static <K, V> MultiMap<K,V> createWithListsAsValues()
    • createWithLinkedSetAsValues

      public static <K, V> MultiMap<K,V> createWithLinkedSetAsValues()
    • put

      @SafeVarargs public final void put(K key, V... values)
      Adds the specified values to the collection the key maps to.
      Parameters:
      key - key indicating the target Collection of the specified value that is to be associated
      values - to be integrated in the Collection of the associated key
    • put

      public void put(K key, Collection<V> values)
      Adds the specified values to the collection the key maps to.
      Parameters:
      key - key indicating the target Collection of the specified value that is to be associated
      values - to be integrated in the Collection of the associated key
    • merge

      public void merge(MultiMap<K,V> otherMultiMap)
      Merges the specified multi map to this multi map. That means: for every entry in the other multi map call put(Object, Collection).
      Parameters:
      otherMultiMap - The map that should be merged into this multi map
    • putReplace

      public void putReplace(K key, Collection<V> values)
      Put the collection of values into the map, identified by the specified key. If the key already maps another collection, the old collection is replaced.
      Parameters:
      key - key indicating the specified values in the map
      values - The collection of values that should be associated with the key
    • remove

      public void remove(Object key, Object value)
      Removes the value in the collection the key maps to.
      Parameters:
      key - to which the Collection is mapped
      value - to be removed from the collection the key maps to
    • remove

      public Collection<V> remove(Object key)
      Removes key and its associated collection from the map.
      Parameters:
      key - to which the Collection is mapped
      Returns:
      The collection that was stored in the list. Returns an empty collection if there was no such key.
    • get

      public Collection<V> get(Object key)
      This method returns collection to which the specified key is mapped. If the key does not exist an empty List is returned.
      Parameters:
      key - The key containing the wanted associated Collection
      Returns:
      the collection to which the specified key is mapped
    • keySet

      public Set<K> keySet()
      Returns the key-set of the internal map. Changes to this set (especially removing values) are reflected in this MultiMap according to Map.keySet().
    • values

      public Collection<V> values()
    • count

      public int count()
      Returns the count of objects contained in this multi map. That means the sum of the size of all value collections.
      Returns:
      The count of objects in this multi map;
    • clear

      public void clear()
      Removes all mappings from this map.
    • asMap

      public Map<K,Collection<V>> asMap()
      Returns an unmodifiable Map containing the entries of this multi map.
      Returns:
      an unmodifiable view of this Map.
    • isEmpty

      public boolean isEmpty()
      Returns true if this multi map contains no key-value pairs.
      Returns:
      true if empty