Class HashNMap<K,​V>

  • All Implemented Interfaces:
    Serializable, Cloneable

    public class HashNMap<K,​V>
    extends Object
    implements Serializable, Cloneable
    The HashNMap can be used to store multiple values by a single key value. The values stored can be retrieved using a direct query or by creating an enumeration over the stored elements.
    Author:
    Thomas Morgner
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      HashNMap()
      Default constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(K key, V val)
      Adds a new key/value pair into this map.
      void clear()
      Clears all keys and values of this map.
      Object clone()
      Creates a deep copy of this HashNMap.
      boolean containsKey​(K key)
      Tests whether this map contains the given key.
      boolean containsValue​(K key, V value)
      Tests whether this map contains the given value.
      boolean containsValue​(V value)
      Tests whether this map contains the given value.
      protected List<V> createList()
      Returns a new empty list.
      V get​(K key, int n)
      Retrieves the n-th value registered for an key or null if there was no such key in the list.
      Iterator<V> getAll​(K key)
      Returns an iterator over all elements registered to the given key.
      V getFirst​(K key)
      Retrieves the first value registered for an key or null if there was no such key in the list.
      V getLast​(K key)
      Retrieves the last value registered for an key or null if there was no such key in the list.
      int getValueCount​(K key)
      Returns the number of elements registered with the given key.
      boolean isEmpty()
      Checks, whether the map is empty.
      Iterator<K> keys()
      Returns all registered keys as an enumeration.
      Set<K> keySet()
      Returns all registered keys as set.
      boolean put​(K key, V val)
      Inserts a new key/value pair into the map.
      boolean remove​(K key, V value)
      Removes the key/value pair from the map.
      void removeAll​(K key)
      Removes all elements for the given key.
      Object[] toArray​(K key)
      Returns the contents for the given key as object array.
      V[] toArray​(K key, V[] data)
      Returns the contents for the given key as object array.
    • Constructor Detail

      • HashNMap

        public HashNMap()
        Default constructor.
    • Method Detail

      • createList

        protected List<V> createList()
        Returns a new empty list.
        Returns:
        A new empty list.
      • put

        public boolean put​(K key,
                           V val)
        Inserts a new key/value pair into the map. If such a pair already exists, it gets replaced with the given values.
        Parameters:
        key - the key.
        val - the value.
        Returns:
        A boolean.
      • add

        public boolean add​(K key,
                           V val)
        Adds a new key/value pair into this map. If the key is not yet in the map, it gets added to the map and the call is equal to put(Object,Object).
        Parameters:
        key - the key.
        val - the value.
        Returns:
        true, if the value has been added, false otherwise
      • getFirst

        public V getFirst​(K key)
        Retrieves the first value registered for an key or null if there was no such key in the list.
        Parameters:
        key - the key.
        Returns:
        the value.
      • getLast

        public V getLast​(K key)
        Retrieves the last value registered for an key or null if there was no such key in the list.
        Parameters:
        key - the key.
        Returns:
        the value.
      • get

        public V get​(K key,
                     int n)
        Retrieves the n-th value registered for an key or null if there was no such key in the list. An index out of bounds exception is thrown if there are less than n elements registered to this key.
        Parameters:
        key - the key.
        n - the index.
        Returns:
        the object.
      • getAll

        public Iterator<V> getAll​(K key)
        Returns an iterator over all elements registered to the given key.
        Parameters:
        key - the key.
        Returns:
        an iterator.
      • keys

        public Iterator<K> keys()
        Returns all registered keys as an enumeration.
        Returns:
        an enumeration of the keys.
      • keySet

        public Set<K> keySet()
        Returns all registered keys as set.
        Returns:
        a set of keys.
      • remove

        public boolean remove​(K key,
                              V value)
        Removes the key/value pair from the map. If the removed entry was the last entry for this key, the key gets also removed.
        Parameters:
        key - the key.
        value - the value.
        Returns:
        true, if removing the element was successfull, false otherwise.
      • removeAll

        public void removeAll​(K key)
        Removes all elements for the given key.
        Parameters:
        key - the key.
      • clear

        public void clear()
        Clears all keys and values of this map.
      • containsKey

        public boolean containsKey​(K key)
        Tests whether this map contains the given key.
        Parameters:
        key - the key.
        Returns:
        true if the key is contained in the map
      • containsValue

        public boolean containsValue​(V value)
        Tests whether this map contains the given value.
        Parameters:
        value - the value.
        Returns:
        true if the value is registered in the map for an key.
      • containsValue

        public boolean containsValue​(K key,
                                     V value)
        Tests whether this map contains the given value.
        Parameters:
        value - the value.
        key - the key under which to find the value
        Returns:
        true if the value is registered in the map for an key.
      • toArray

        public V[] toArray​(K key,
                           V[] data)
        Returns the contents for the given key as object array. If there were no objects registered with that key, an empty object array is returned.
        Parameters:
        key - the key.
        data - the object array to receive the contents.
        Returns:
        the contents.
      • toArray

        public Object[] toArray​(K key)
        Returns the contents for the given key as object array. If there were no objects registered with that key, an empty object array is returned.
        Parameters:
        key - the key.
        Returns:
        the contents.
      • getValueCount

        public int getValueCount​(K key)
        Returns the number of elements registered with the given key.
        Parameters:
        key - the key.
        Returns:
        the number of element for this key, or 0 if there are no elements registered.
      • isEmpty

        public boolean isEmpty()
        Checks, whether the map is empty.
        Returns:
        true, if the map does not contain any keys.