Class MultiHashMap<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of the values stored per key
All Implemented Interfaces:
Serializable, Cloneable, Map<K, ArrayList<V>>, SequencedMap<K, ArrayList<V>>

public class MultiHashMap<K,V> extends LinkedHashMap<K, ArrayList<V>>
A map that can hold several values per key. Each key is mapped to an ArrayList of values; the keys keep their insertion order (based on LinkedHashMap). Use putEntry(Object, Object) and removeEntry(Object, Object) to add or remove single values.

Note: size() is overridden and returns the total number of values, not the number of keys. The class is not synchronized.

See Also:
  • Constructor Details

    • MultiHashMap

      public MultiHashMap()
  • Method Details

    • keysSize

      public int keysSize()
      Intended to return the number of keys. Note: because size() is overridden, this currently returns the total number of values as well.
      Returns:
      the number of values (see note)
    • valueSize

      public int valueSize()
      Returns:
      the total number of values over all keys
    • size

      public int size()
      Returns the total number of values over all keys (not the number of keys).
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class HashMap<K, ArrayList<V>>
      See Also:
    • containsValue

      public boolean containsValue(Object value)
      Checks whether a single value is stored under any key (not whether a value list is stored).
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class LinkedHashMap<K, ArrayList<V>>
    • allValues

      public Collection<V> allValues()
      Returns:
      a new collection with the values of all keys, in key order
    • putEntry

      public V putEntry(K key, V value)
      Adds a value to the list of a key; the list is created if necessary.
      Parameters:
      key - the key
      value - the value to add
      Returns:
      value
    • removeEntry

      public void removeEntry(K key, V value)
      Removes a value from the list of a key. The key is removed when its list becomes empty.
      Parameters:
      key - the key
      value - the value to remove