Class AbstractDeque<E>

  • Type Parameters:
    E - The type of elements held in this deque
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.Deque<E>, java.util.Queue<E>
    Direct Known Subclasses:
    EmptyDeque, SingletonDeque

    public abstract class AbstractDeque<E>
    extends java.util.AbstractQueue<E>
    implements java.util.Deque<E>
    Abstract Deque implementation that provides default implementations for some of the operations in the Deque interface.

    This class extends AbstractQueue, which itself extends AbstractCollection, and hence inherits behaviors such as size, isEmpty, etc. Subclasses need to implement the abstract methods defined by the deque contract, such as insertion, removal, and access methods.

    The default implementations in this class make assumptions about the behavior of other methods, so subclasses should ensure consistent behavior when overriding any of these methods.

    Example Usage

    
     public class SimpleDeque&lt;E&gt; extends AbstractDeque&lt;E&gt; {
         private final List&lt;E&gt; list = new ArrayList&lt;&gt;();
    
         public void addFirst(E e) {
             list.add(0, e);
         }
    
         public void addLast(E e) {
             list.add(e);
         }
    
         public E peekFirst() {
             return list.isEmpty() ? null : list.get(0);
         }
    
         public E peekLast() {
             return list.isEmpty() ? null : list.get(list.size() - 1);
         }
    
         public E pollFirst() {
             return list.isEmpty() ? null : list.remove(0);
         }
    
         public E pollLast() {
             return list.isEmpty() ? null : list.remove(list.size() - 1);
         }
    
         public boolean removeFirstOccurrence(Object o) {
             return list.remove(o);
         }
    
         public boolean removeLastOccurrence(Object o) {
             int index = list.lastIndexOf(o);
             if (index != -1) {
                 list.remove(index);
                 return true;
             }
             return false;
         }
     }
      
    Since:
    1.0.0
    Author:
    Mercy
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractDeque()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFirst​(E e)  
      void addLast​(E e)  
      boolean offer​(E e)  
      E peek()  
      E poll()  
      E pop()  
      void push​(E e)  
      E removeFirst()  
      boolean removeFirstOccurrence​(java.lang.Object o)  
      E removeLast()  
      • Methods inherited from class java.util.AbstractQueue

        add, addAll, clear, element, remove
      • Methods inherited from class java.util.AbstractCollection

        contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        addAll, clear, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
      • Methods inherited from interface java.util.Deque

        add, contains, descendingIterator, element, getFirst, getLast, iterator, offerFirst, offerLast, peekFirst, peekLast, pollFirst, pollLast, remove, remove, removeLastOccurrence, size
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Constructor Detail

      • AbstractDeque

        public AbstractDeque()
    • Method Detail

      • addFirst

        public void addFirst​(E e)
        Specified by:
        addFirst in interface java.util.Deque<E>
      • addLast

        public void addLast​(E e)
        Specified by:
        addLast in interface java.util.Deque<E>
      • removeFirst

        public E removeFirst()
        Specified by:
        removeFirst in interface java.util.Deque<E>
      • removeLast

        public E removeLast()
        Specified by:
        removeLast in interface java.util.Deque<E>
      • removeFirstOccurrence

        public boolean removeFirstOccurrence​(java.lang.Object o)
        Specified by:
        removeFirstOccurrence in interface java.util.Deque<E>
      • push

        public void push​(E e)
        Specified by:
        push in interface java.util.Deque<E>
      • pop

        public E pop()
        Specified by:
        pop in interface java.util.Deque<E>
      • offer

        public boolean offer​(E e)
        Specified by:
        offer in interface java.util.Deque<E>
        Specified by:
        offer in interface java.util.Queue<E>
      • poll

        public E poll()
        Specified by:
        poll in interface java.util.Deque<E>
        Specified by:
        poll in interface java.util.Queue<E>
      • peek

        public E peek()
        Specified by:
        peek in interface java.util.Deque<E>
        Specified by:
        peek in interface java.util.Queue<E>