Class HashDequeMap<K,​V>

  • Type Parameters:
    K - the key type.
    V - the value type stored.
    All Implemented Interfaces:
    Serializable, Cloneable, Map<K,​Deque<V>>

    public class HashDequeMap<K,​V>
    extends HashMap<K,​Deque<V>>
    A hash map that stores deques of a particular type.
    Author:
    Matthew Tropiano
    See Also:
    Serialized Form
    • Constructor Detail

      • HashDequeMap

        public HashDequeMap()
        Creates a new HashDequeMap that has default capacity and load factor.
      • HashDequeMap

        public HashDequeMap​(int initialCapacity)
        Creates a new HashDequeMap that has a specific capacity and default load factor.
        Parameters:
        initialCapacity - the initial capacity.
      • HashDequeMap

        public HashDequeMap​(int initialCapacity,
                            float loadFactor)
        Creates a new HashDequeMap that has a specific capacity and default load factor.
        Parameters:
        initialCapacity - the initial capacity.
        loadFactor - the load factor.
    • Method Detail

      • create

        protected Deque<V> create()
        Called to create a new Deque implementation that gets stored in the table. By default, this calls new LinkedList<>().
        Returns:
        a new deque.
      • addFirst

        public void addFirst​(K key,
                             V value)
        Adds a value to the beginning of a deque. If no corresponding deque, this creates a new deque.
        Parameters:
        key - the key.
        value - the value.
        See Also:
        Deque.addFirst(Object)
      • addLast

        public void addLast​(K key,
                            V value)
        Adds a value to the end of a deque. If no corresponding deque, this creates a new deque.
        Parameters:
        key - the key.
        value - the value.
        See Also:
        Deque.addLast(Object)
      • removeFirst

        public V removeFirst​(K key)
        Removes a value from the beginning of a deque. If no corresponding deque, this throws an exception.
        Parameters:
        key - the key.
        Returns:
        the element removed.
        Throws:
        NoSuchElementException - if the key does not correspond to an existing deque.
        See Also:
        Deque.removeFirst()
      • removeLast

        public V removeLast​(K key)
        Removes a value from the end of a deque. If no corresponding deque, this throws an exception.
        Parameters:
        key - the key.
        Returns:
        the element removed.
        Throws:
        NoSuchElementException - if the key does not correspond to an existing deque.
        See Also:
        Deque.removeLast()
      • pollFirst

        public V pollFirst​(K key)
        Removes a value from the beginning of a deque. If no corresponding deque, this returns null.
        Parameters:
        key - the key.
        Returns:
        the element removed, or null if no element.
        See Also:
        Deque.pollFirst()
      • pollLast

        public V pollLast​(K key)
        Removes a value from the end of a deque. If no corresponding deque, this returns null.
        Parameters:
        key - the key.
        Returns:
        the element removed, or null if no element.
        See Also:
        Deque.pollLast()
      • peekFirst

        public V peekFirst​(K key)
        Returns the value at the beginning of a deque. If no corresponding deque, this returns null.
        Parameters:
        key - the key.
        Returns:
        the element found, or null if no element.
        See Also:
        Deque.peekFirst()
      • peekLast

        public V peekLast​(K key)
        Returns the value at the end of a deque. If no corresponding deque, this returns null.
        Parameters:
        key - the key.
        Returns:
        the element found, or null if no element.
        See Also:
        Deque.peekLast()
      • add

        public boolean add​(K key,
                           V value)
        Adds a value to the end of a deque. If no corresponding deque, this creates a new deque.
        Parameters:
        key - the key.
        value - the value.
        Returns:
        true (as specified by Collection.add(Object))
        See Also:
        Deque.add(Object)
      • removeValue

        public boolean removeValue​(K key,
                                   V value)
        Removes a value from a deque. If no corresponding deque, this returns false.
        Parameters:
        key - the key.
        value - the value to remove.
        Returns:
        true if an element was removed as a result of this call
        Throws:
        ClassCastException - if the class of the specified element is incompatible with the deque (optional)
        NullPointerException - if the specified element is null and this deque does not permit null elements (optional)
        See Also:
        Deque.remove(Object)
      • pop

        public V pop​(K key)
        Removes a value from the front of a deque. If no corresponding deque, this throws an exception.
        Parameters:
        key - the key.
        Returns:
        the element removed.
        Throws:
        NoSuchElementException - if the key does not correspond to an existing deque.
        See Also:
        Deque.pop()
      • poll

        public V poll​(K key)
        Removes a value from the front of a deque. If no corresponding deque, this returns null.
        Parameters:
        key - the key.
        Returns:
        the element removed, or null if no element.
        See Also:
        Deque.poll()
      • push

        public void push​(K key,
                         V value)
        Adds a value to the front of a deque. If no corresponding deque, this creates it.
        Parameters:
        key - the key.
        value - the value.
        See Also:
        Deque.push(Object)