Class MultiMap<K,​V>

  • Type Parameters:
    K - the type of the keys the map uses.
    V - the type of the values the map uses.

    public class MultiMap<K,​V>
    extends java.lang.Object
    A lightweight implementation of a MultiMap, that is a Map that is able to hold multiple values for every key.

    This MultiMap uses a LinkedHashMap together with a ArrayList in order to keep the order of its entries.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_MAP_SIZE
      The constant value 6.
    • Constructor Summary

      Constructors 
      Constructor Description
      MultiMap()
      Constructs a new MultiMap with a initial capacity of DEFAULT_MAP_SIZE.
      MultiMap​(int size)
      Constructs a new MultiMap.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      MultiMap<K,​V> asUnmodifiableMultiMap()  
      void clear()  
      MultiMap<K,​V> clone()  
      boolean containsKey​(K key)  
      boolean containsValue​(V value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      java.util.List<V> getAll​(K key)
      Get all values for the given key.
      V getFirst​(K key)
      Get the first value for the given key, or null if there are no entries.
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      boolean put​(K key, V value)  
      void putAll​(java.util.Map<? extends K,​? extends V> map)  
      boolean putFirst​(K key, V value)  
      V remove​(K key)
      Removes all mappings for the given key and returns the first value if there where mappings or null if not.
      java.util.List<V> remove​(K key, int num)
      Remove the given number of values for a given key.
      boolean removeOne​(K key, V value)
      Remove the mapping of the given key to the value.
      int size()  
      java.util.List<V> values()
      Returns a new list containing all values of this multi map.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MultiMap

        public MultiMap​(int size)
        Constructs a new MultiMap.
        Parameters:
        size - the initial capacity.
    • Method Detail

      • size

        public int size()
      • isEmpty

        public boolean isEmpty()
      • getFirst

        public V getFirst​(K key)
        Get the first value for the given key, or null if there are no entries.
        Parameters:
        key - TODO javadoc me please
        Returns:
        the first value or null.
      • getAll

        public java.util.List<VgetAll​(K key)
        Get all values for the given key. Returns the empty set if there are none.

        Changes to the returned set will update the underlying MultiMap if the return set is not empty.

        Parameters:
        key - TODO javadoc me please
        Returns:
        all values for the given key.
      • put

        public boolean put​(K key,
                           V value)
      • putFirst

        public boolean putFirst​(K key,
                                V value)
      • remove

        public V remove​(K key)
        Removes all mappings for the given key and returns the first value if there where mappings or null if not.
        Parameters:
        key - TODO javadoc me please
        Returns:
        the first value of the given key or null.
      • removeOne

        public boolean removeOne​(K key,
                                 V value)
        Remove the mapping of the given key to the value.

        Returns true if the mapping was removed and false if the mapping did not exist.

        Parameters:
        key - TODO javadoc me please
        value - TODO javadoc me please
        Returns:
        true if the mapping was removed, false otherwise.
      • remove

        public java.util.List<Vremove​(K key,
                                        int num)
        Remove the given number of values for a given key. May return less values then requested.
        Parameters:
        key - the key to remove from.
        num - the number of values to remove.
        Returns:
        a list of the removed values.
        Since:
        4.4.0
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> map)
      • clear

        public void clear()
      • keySet

        public java.util.Set<KkeySet()
      • values

        public java.util.List<Vvalues()
        Returns a new list containing all values of this multi map.
        Returns:
        a new list with all values.
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      • clone

        public MultiMap<K,​Vclone()
        Overrides:
        clone in class java.lang.Object