Class MultiMap<A,​B>

  • Type Parameters:
    A - Key type
    B - Value type

    public class MultiMap<A,​B>
    extends java.lang.Object
    MultiMap - maps keys to multiple values.

    Corresponds to Google guava LinkedHashMultimap and Apache Collections MultiValueMap but it is an independent (simple) implementation.

    Since:
    2702
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<A,​java.util.Set<B>> map  
    • Constructor Summary

      Constructors 
      Constructor Description
      MultiMap()
      Constructs a new MultiMap.
      MultiMap​(int capacity)
      Constructs a new MultiMap with the specified initial capacity.
      MultiMap​(java.util.Map<A,​java.util.Set<B>> map0)
      Constructs a new MultiMap from an ordinary Map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all of the mappings from this map.
      boolean contains​(A key, B value)
      Returns true if the multimap contains a value for a key.
      boolean containsKey​(A key)
      Returns true if this map contains a mapping for the specified key.
      java.util.Set<java.util.Map.Entry<A,​java.util.Set<B>>> entrySet()
      Returns a Set view of the mappings contained in this map.
      boolean equals​(java.lang.Object obj)  
      java.util.Set<B> get​(A key)
      Returns the Set associated with the given key.
      java.util.Set<B> getValues​(A key)
      Like get, but returns an empty Set if nothing has been mapped to the key.
      int hashCode()  
      boolean isEmpty()
      Returns true if this map contains no key-value mappings.
      java.util.Set<A> keySet()
      Get the keySet.
      void put​(A key, B value)
      Map a key to a value.
      void putAll​(A key, java.util.Collection<B> values)
      Map the key to all the given values.
      void putVoid​(A key)
      Put a key that maps to nothing.
      java.util.Set<B> remove​(A key)
      Removes all mappings for a certain key.
      boolean remove​(A key, B value)
      Removes a certain key=value mapping.
      int size()
      Returns the number of keys.
      java.util.Map<A,​java.util.Set<B>> toMap()
      Converts this MultiMap to a Map with Set values.
      java.lang.String toString()  
      java.util.Collection<java.util.Set<B>> values()
      Returns a collection of all value sets.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • map

        private final java.util.Map<A,​java.util.Set<B>> map
    • Constructor Detail

      • MultiMap

        public MultiMap()
        Constructs a new MultiMap.
      • MultiMap

        public MultiMap​(int capacity)
        Constructs a new MultiMap with the specified initial capacity.
        Parameters:
        capacity - the initial capacity
      • MultiMap

        public MultiMap​(java.util.Map<A,​java.util.Set<B>> map0)
        Constructs a new MultiMap from an ordinary Map.
        Parameters:
        map0 - the Map
    • Method Detail

      • put

        public void put​(A key,
                        B value)
        Map a key to a value.

        Can be called multiple times with the same key, but different value.

        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
      • putVoid

        public void putVoid​(A key)
        Put a key that maps to nothing. (Only if it is not already in the map)

        Afterwards containsKey(key) will return true and get(key) will return an empty Set instead of null.

        Parameters:
        key - key with which an empty set is to be associated
      • putAll

        public void putAll​(A key,
                           java.util.Collection<B> values)
        Map the key to all the given values.

        Adds to the mappings that are already there.

        Parameters:
        key - key with which the specified values are to be associated
        values - values to be associated with the specified key
      • keySet

        public java.util.Set<AkeySet()
        Get the keySet.
        Returns:
        a set view of the keys contained in this map
        See Also:
        Map.keySet()
      • get

        public java.util.Set<Bget​(A key)
        Returns the Set associated with the given key. Result is null if nothing has been mapped to this key.

        Modifications of the returned list changes the underling map, but you should better not do that.

        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the set of values to which the specified key is mapped, or null if this map contains no mapping for the key
        See Also:
        Map.get(Object)
      • getValues

        public java.util.Set<BgetValues​(A key)
        Like get, but returns an empty Set if nothing has been mapped to the key.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the set of values to which the specified key is mapped, or an empty set if this map contains no mapping for the key
      • isEmpty

        public boolean isEmpty()
        Returns true if this map contains no key-value mappings.
        Returns:
        true if this map contains no key-value mappings
        See Also:
        Map.isEmpty()
      • containsKey

        public boolean containsKey​(A key)
        Returns true if this map contains a mapping for the specified key.
        Parameters:
        key - key whose presence in this map is to be tested
        Returns:
        true if this map contains a mapping for the specified key
        See Also:
        Map.containsKey(Object)
      • contains

        public boolean contains​(A key,
                                B value)
        Returns true if the multimap contains a value for a key.
        Parameters:
        key - The key
        value - The value
        Returns:
        true if the key contains the value
      • clear

        public void clear()
        Removes all of the mappings from this map. The map will be empty after this call returns.
        See Also:
        Map.clear()
      • entrySet

        public java.util.Set<java.util.Map.Entry<A,​java.util.Set<B>>> entrySet()
        Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
        Returns:
        a set view of the mappings contained in this map
        See Also:
        Map.entrySet()
      • size

        public int size()
        Returns the number of keys.
        Returns:
        the number of key-value mappings in this map
        See Also:
        Map.size()
      • values

        public java.util.Collection<java.util.Set<B>> values()
        Returns a collection of all value sets.
        Returns:
        a collection view of the values contained in this map
        See Also:
        Map.values()
      • remove

        public boolean remove​(A key,
                              B value)
        Removes a certain key=value mapping.
        Parameters:
        key - key whose mapping is to be removed from the map
        value - value whose mapping is to be removed from the map
        Returns:
        true, if something was removed
      • remove

        public java.util.Set<Bremove​(A key)
        Removes all mappings for a certain key.
        Parameters:
        key - key whose mapping is to be removed from the map
        Returns:
        the previous value associated with key, or null if there was no mapping for key.
        See Also:
        Map.remove(Object)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toMap

        public java.util.Map<A,​java.util.Set<B>> toMap()
        Converts this MultiMap to a Map with Set values.
        Returns:
        the converted Map
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object