Class MultiHashMap<K,V>

java.lang.Object
net.okapia.osid.torrefacto.collect.MultiHashMap<K,V>
Type Parameters:
K - the key type
V - the value type
All Implemented Interfaces:
MultiMap<K,V>
Direct Known Subclasses:
IdMultiHashMap, TypeMultiHashMap

public class MultiHashMap<K,V> extends Object implements MultiMap<K,V>
A MultiMap that maps each key to a set of values, backed by a LinkedHashMap of keys to LinkedHashSets. Both the keys and the values under a key keep their insertion order. The value collections are sets, so a key cannot map to the same value twice; a repeated put of an existing key/value pair is a no-op. A key is dropped once its last value is removed, so containsKey(K) returns false for it thereafter. Null keys and null values are rejected. size() reports the total number of key/value pairs (a value shared by two keys counts twice), while values() returns the distinct values across all keys. The collections returned by get(K), keySet(), values(), and asMap() are unmodifiable. This implementation is not synchronized.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty MultiHashMap with the default initial capacity (16) and the default load factor (0.75).
    MultiHashMap(int initialCapacity)
    Constructs an empty MultiHashMap with the specified initial capacity and the default load factor (0.75).
    MultiHashMap(int initialCapacity, float loadFactor)
    Constructs an empty MultiHashMap with the specified initial capacity and load factor.
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets a Map of this MultiMap.
    void
    Removes all of the mappings from this map.
    boolean
    Tests if this map contains a mapping for the specified key.
    boolean
    containsKeyValue(K key, V value)
    Tests the map contains the key and value specified.
    boolean
    Tests the map contains the value specified.
    get(K key)
    Returns the values to which the specified key is mapped.
    boolean
    Tests if this map contains no key-value mappings.
    boolean
    Tests if this map can be modified.
    Returns an immutable Collection view of the keys contained in this map.
    void
    put(K key, V value)
    Associates the specified value with the specified key in this map.
    void
    putAll(Map<K, Collection<? extends V>> map)
    Stores a map of keys and values inside this map.
    void
    putAll(K key, Collection<? extends V> values)
    Adds a collection of values to to a key in this map.
    void
    putAll(MultiMap<K, ? extends V> map)
    Stores a Multimap inside this map.
    void
    remove(K key)
    Removes the mapping for a key and all its related values.
    void
    remove(K key, V value)
    Removes a specific key/value pair from the map.
    void
    removeValue(V value)
    Removes a value from all keys.
    long
    Returns the number of key-value mappings in this map.
    Returns a Collection view of the values contained in this map.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MultiHashMap

      public MultiHashMap(int initialCapacity, float loadFactor)
      Constructs an empty MultiHashMap with the specified initial capacity and load factor.
      Parameters:
      initialCapacity - the initial capacity
      loadFactor - the load factor
      Throws:
      IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive
    • MultiHashMap

      public MultiHashMap(int initialCapacity)
      Constructs an empty MultiHashMap with the specified initial capacity and the default load factor (0.75).
      Parameters:
      initialCapacity - the initial capacity.
      Throws:
      IllegalArgumentException - if the initial capacity is negative.
    • MultiHashMap

      public MultiHashMap()
      Constructs an empty MultiHashMap with the default initial capacity (16) and the default load factor (0.75).
  • Method Details

    • isModifiable

      public boolean isModifiable()
      Tests if this map can be modified.
      Specified by:
      isModifiable in interface MultiMap<K,V>
      Returns:
      true if the map can be modified, false if read-only
    • size

      public long size()
      Returns the number of key-value mappings in this map.
      Specified by:
      size in interface MultiMap<K,V>
      Returns:
      the number of key-value mappings in this map
    • isEmpty

      public boolean isEmpty()
      Tests if this map contains no key-value mappings.
      Specified by:
      isEmpty in interface MultiMap<K,V>
      Returns:
      true if this map contains no key-value mappings, false otherwise
    • containsKey

      public boolean containsKey(K key)
      Tests if this map contains a mapping for the specified key.
      Specified by:
      containsKey in interface MultiMap<K,V>
      Parameters:
      key - the key whose presence in this map is to be tested
      Returns:
      true if this map contains a mapping for the specified key, false otherwise
      Throws:
      NullArgumentException - key is null
    • containsValue

      public boolean containsValue(V value)
      Tests the map contains the value specified.
      Specified by:
      containsValue in interface MultiMap<K,V>
      Parameters:
      value - the value to search for
      Returns:
      true if the map contains the value, false otherwise
      Throws:
      NullArgumentException - value is null
    • containsKeyValue

      public boolean containsKeyValue(K key, V value)
      Tests the map contains the key and value specified.
      Specified by:
      containsKeyValue in interface MultiMap<K,V>
      Parameters:
      key - the key
      value - the value
      Returns:
      true if the map contains the key/value pair, false otherwise
      Throws:
      NullArgumentException - key or value is null
    • get

      public Collection<V> get(K key)
      Returns the values to which the specified key is mapped. If the key is not defined, this method returns an empty list.
      Specified by:
      get in interface MultiMap<K,V>
      Parameters:
      key - the key
      Returns:
      a collection of the values for the given key or an empty collection if the key is not found
      Throws:
      NullArgumentException - key is null
    • put

      public void put(K key, V value)
      Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
      Specified by:
      put in interface MultiMap<K,V>
      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
      Throws:
      NullArgumentException - key or value is null
    • putAll

      public void putAll(K key, Collection<? extends V> values)
      Adds a collection of values to to a key in this map.
      Specified by:
      putAll in interface MultiMap<K,V>
      Parameters:
      key - the key
      values - the values
      Throws:
      NullArgumentException - key or values is null
    • putAll

      public void putAll(Map<K, Collection<? extends V>> map)
      Stores a map of keys and values inside this map.
      Specified by:
      putAll in interface MultiMap<K,V>
      Parameters:
      map - the map to store
      Throws:
      NullArgumentException - map is null
    • putAll

      public void putAll(MultiMap<K, ? extends V> map)
      Stores a Multimap inside this map.
      Specified by:
      putAll in interface MultiMap<K,V>
      Parameters:
      map - the map to store
      Throws:
      NullArgumentException - map is null
    • remove

      public void remove(K key)
      Removes the mapping for a key and all its related values.
      Specified by:
      remove in interface MultiMap<K,V>
      Parameters:
      key - whose mapping is to be removed from the map
      Throws:
      NullArgumentException - key is null
    • remove

      public void remove(K key, V value)
      Removes a specific key/value pair from the map. The item is removed from the collection mapped to the specified key. Other values attached to that key are unaffected.
      Specified by:
      remove in interface MultiMap<K,V>
      Parameters:
      key - the key
      value - the value to remove
      Throws:
      NullArgumentException - key or value is null
    • removeValue

      public void removeValue(V value)
      Removes a value from all keys.
      Specified by:
      removeValue in interface MultiMap<K,V>
      Parameters:
      value - to be removed from the map
      Throws:
      NullArgumentException - value is null
    • clear

      public void clear()
      Removes all of the mappings from this map. The map will be empty after this call returns.
      Specified by:
      clear in interface MultiMap<K,V>
    • keySet

      public Collection<K> keySet()
      Returns an immutable Collection view of the keys contained in this map.
      Specified by:
      keySet in interface MultiMap<K,V>
      Returns:
      a set view of the keys contained in this map
    • values

      public Collection<V> values()
      Returns a Collection view of the values contained in this map.
      Specified by:
      values in interface MultiMap<K,V>
      Returns:
      a collection of the values
    • asMap

      public Map<K, Collection<V>> asMap()
      Gets a Map of this MultiMap. Neither the returned map nor its value collections can be modified.
      Specified by:
      asMap in interface MultiMap<K,V>
      Returns:
      a map view of this MultiMap