1. Interface definition

A linear collection that supports inserting and removing elements at both ends (Double Ended Queue, Deque, D). Most Deque implementations do not have a fixed limit on the number of elements they may contain, but this interface supports both capabit and Deque implementations that do not have a fixed capacity limit.

This interface defines methods to access elements on both sides of the Deque, which are provided for insert, extract, and retrieve operations. Each of these operation methods comes in one of two forms: either to throw an exception when the operation fails, or to return a special value (either null or false, depending on the operation). The latter form of insert operation is designed specifically for Deque implementations using capacity limits; In most implementations, the insert operation does not fail.

An exception is thrown Return special value
Insert (Insert) addFirst(e)

addLast(e)
offerFirst(e)

offerLast(e)
Remove/Remove removeFirst()

removeLast()
pollFirst()

pollLast()
Search/Examine (Examine) getFirst()

getLast()
peekFirst()

peekLast()

This interface inherits the Queue interface. When a Deque is used as a Queue, FIFO (first in, first out) behavior occurs: elements are added to the end of the Deque and removed from the head. The methods inherited from the Queue interface are exactly the same as the Deque methods, as shown in the table below:

The Queue method The equivalent Deque method
add(e) addLast(e)
offer(e) offerLast(e)
remove() removeFirst()
poll() pollFirst()
element() getFirst()
peek() peekFirst()

A Deque can also be used as a LIFO (last in, first out) Stack. This interface should be used in preference to the legacy java.util.Stack class. When a Deque is used as a stack, elements are pushed in and out of the head of the Deque. Stack method is completely equivalent to Deque method, as shown in the following table:

The Stack method The equivalent Deque method
push(e) addFirst(e)
pop() removeFirst()
peek() peekFirst()

Note that the peek() method works equally well when a Deque is used as a Queue or Stack; In both cases, the element is extracted from the head of the Deque.

The interface also provides two methods to remove internal elements, removeFirstCurrence () and removeVelastOccurrence ().

Unlike the java.util.List interface, this interface does not support indexed access to elements.

Although the Deque implementation does not strictly prohibit the insertion of null elements, it is strongly recommended to do so. Any user of a Deque implementation that allows null elements is strongly advised not to take advantage of the ability to insert null elements. This is because null is used by various methods as a special return value to indicate that the Deque is empty.

Deque implementations typically do not define element-based versions of equals() and hashCode(), but instead inherit identity-based versions of methods from class Object.

Java.util.deque inherits from the java.util.queue interface. The interface inheritance relationship is shown in the figure below:

2. Method declarations

1) void addFirst(E E);

  • Inserts the specified element into it immediately without violating the capacity limitDequeIn the head, when using a capacity limitDequeIs usually best usedofferFirst()Methods;
  • Thrown if the element cannot be added at this point due to capacity constraints (no free space)java.lang.IllegalStateException;
  • Thrown if the class of the specified element prevents it from being added to this queuejava.lang.ClassCastException;
  • If the specified element isnullAnd the queue is not allowednullElement is thrownjava.lang.NullPointerException;
  • Thrown if some attribute of this element prevents it from being added to this queuejava.lang.IllegalArgumentException.

2) void addLast(E E);

  • Inserts the specified element into it immediately without violating the capacity limitDequeIn the tail, when using a capacity limitDequeIs usually best usedofferLast()Methods;
  • Thrown if the element cannot be added at this point due to capacity constraints (no free space)java.lang.IllegalStateException;
  • Thrown if the class of the specified element prevents it from being added to this queuejava.lang.ClassCastException;
  • If the specified element isnullAnd the queue is not allowednullElement is thrownjava.lang.NullPointerException;
  • Thrown if some attribute of this element prevents it from being added to this queuejava.lang.IllegalArgumentException.

3) Boolean offerFirst(E E);

  • Inserts the specified element intoDequeThe header, unless it violates capacity limits, is returned on successtrue, otherwise returnfalse. This method is usually better than that when using a limited capacity queueaddFirst()It’s better to use, because when you can’t add elements,addFirst()Methods can only throw exceptions;
  • Thrown if the class of the specified element prevents it from being added to this queuejava.lang.ClassCastException;
  • If the specified element isnullAnd the queue is not allowednullElement is thrownjava.lang.NullPointerException;
  • Thrown if some attribute of this element prevents it from being added to this queuejava.lang.IllegalArgumentException.

4) Boolean offerLast(E E);

  • Inserts the specified element intoDequeThe tail of, unless it violates the capacity limit, is returned on successtrue, otherwise returnfalse. This method is usually better than that when using a limited capacity queueaddLast()It’s better to use, because when you can’t add elements,addLast()Methods can only throw exceptions;
  • Thrown if the class of the specified element prevents it from being added to this queuejava.lang.ClassCastException;
  • If the specified element isnullAnd the queue is not allowednullElement is thrownjava.lang.NullPointerException;
  • Thrown if some attribute of this element prevents it from being added to this queuejava.lang.IllegalArgumentException.

5) E removeFirst ();

  • Retrieves and removes theDequeThe first element (header element) of the With this methodpollFirst()The difference is: when theDequeIs null, then an exception is thrown;
  • If theDequeIf it is empty, it is thrownjava.util.NoSuchElementException.

6) E removeLast ();

  • Retrieves and removes theDequeThe last element of the. With this methodpollLast()The difference is: when theDequeIs null, then an exception is thrown;
  • If theDequeIf it is empty, it is thrownjava.util.NoSuchElementException.

7) E pollFirst ();

  • Retrieves and removes theDequeThe first element (header element) of theDequeIs empty, it is returnednull.

8) E pollLast ();

  • Retrieves and removes theDequeThe last element (tail element) of theDequeIs empty, it is returnednull.

9) E getFirst ();

  • Retrieves (but does not remove) theDequeThe first element (header element) of the With this methodpeekFirst()The difference is: when theDequeIs null, then an exception is thrown;
  • If theDequeIf it is empty, it is thrownjava.util.NoSuchElementException.

10) E getLast ();

  • Retrieves (but does not remove) theDequeThe last element of the. With this methodpeekLast()The difference is: when theDequeIs null, then an exception is thrown;
  • If theDequeIf it is empty, it is thrownjava.util.NoSuchElementException.

11) E peekFirst ();

  • Retrieves (but does not remove) theDequeThe first element (header element) of theDequeIs empty, it is returnednull.

12) E peekLast ();

  • Retrieves (but does not remove) theDequeThe last element (tail element) of theDequeIs empty, it is returnednull.

13) Boolean RemoveFirstStoccurrence (Object O);

  • fromDequeRemoves the first match of the specified element from the If theDequeIf the element is not included in, it will not change. More formally, remove the first satisfaction(o==null ? e==null : o.equals(e))Element of an expressione(if such an element exists). If theDequeContains the specified element, removes it and returnstrue;
  • If you specify the type of the element andDequeIf not compatible, throwjava.lang.ClassCastException(optional);
  • If the specified element isnullAnd thisDequeIt is not allowed tonullElement is thrownjava.lang.NullPointerException(Optional).

Note: “Optional” means that the specific Deque implementation can either return this exception, or return success, depending on its implementation purpose to choose the appropriate policy.

14) Boolean removelastOccurrence (Object o);

  • fromDequeRemoves the last match of the specified element from the If theDequeIf the element is not included in, it will not change. More formally, remove the last satisfaction(o==null ? e==null : o.equals(e))Element of an expressione(if such an element exists). If theDequeContains the specified element, removes it and returnstrue;
  • If you specify the type of the element andDequeIf not compatible, throwjava.lang.ClassCastException(optional);
  • If the specified element isnullAnd thisDequeIt is not allowed tonullElement is thrownjava.lang.NullPointerException(Optional).

The following is the Queue method

15) Boolean add(E E);

  • If the specified element can be inserted into it immediately without violating the capacity limitDequeRepresented by theQueueQueue (in other words, insert into theDequeIs returned on successful insertiontrue(e.g.,Collection.add()Specified) when the use capacity is limitedDequeIs usually best usedoffer()Methods.With this methodaddLast()Methods the equivalent;
  • Thrown if the element cannot be added at this point due to capacity constraints (no free space)java.lang.IllegalStateException;
  • Thrown if the class of the specified element prevents it from being added to this queuejava.lang.ClassCastException;
  • If the specified element isnullAnd the queue is not allowednullElement is thrownjava.lang.NullPointerException;
  • Thrown if some attribute of this element prevents it from being added to this queuejava.lang.IllegalArgumentException.

16) Boolean offer(E E);

  • If the specified element can be inserted into it immediately without violating the capacity limitDequeRepresented by theQueueQueue (in other words, insert into theDequeIs returned on successful insertiontrue, otherwise returnfalse, when using a limited capacityDequeThis method is usually better thanadd()Better to use, because when an element cannot be added due to capacity constraints (no free space),add()Methods can only throw exceptions.With this methodofferLast()Methods the equivalent;
  • Thrown if the class of the specified element prevents it from being added to this queuejava.lang.ClassCastException;
  • If the specified element isnullAnd the queue is not allowednullElement is thrownjava.lang.NullPointerException;
  • Thrown if some attribute of this element prevents it from being added to this queuejava.lang.IllegalArgumentException.

17) E the remove ();

  • Retrieve and remove thisDequeRepresented by theQueueThe header element of the queue (in other words, remove theDequeOf the first element), this method is associated withpoll()The difference is that if the queue is empty, an exception is thrown.With this methodremoveFirst()Methods the equivalent;
  • If the queue is empty, it is thrownjava.util.NoSuchElementException.

18) E poll ();

  • Retrieve and remove thisDequeRepresented by theQueueThe header element of the queue (in other words, remove theDequeReturns if the queue is emptynull.With this methodpollFirst()Methods the equivalent.

19) E element ();

  • Retrieves (but does not remove) thisDequeRepresented by theQueueThe header element of the queue (in other words, retrieve theDequeOf the first element), this method is associated withpeek()The difference is that if the queue is empty, an exception is thrown.With this methodgetFirst()Methods the equivalent;
  • If the queue is empty, it is thrownjava.util.NoSuchElementException.

20) E peek ();

  • Retrieves (but does not remove) thisDequeRepresented by theQueueThe header element of the queue (in other words, retrieve theDequeReturns if the queue is emptynull.With this methodpeekFirst()Methods the equivalent.

Here is the Stack Stack method

21) void push(E E);

  • Push an element into it immediately without violating the capacity limitDequeRepresented by theStackStack (in other words, push into theDequeOf the head).With this methodaddFirst()Methods the equivalent;
  • Thrown if the element cannot be added at this point due to capacity constraints (no free space)java.lang.IllegalStateException;
  • Thrown if the class of the specified element prevents it from being added to this stackjava.lang.ClassCastException;
  • If the specified element isnullAnd the stack is not allowednullElement is thrownjava.lang.NullPointerException;
  • Thrown if some attribute of this element prevents it from being added to this stackjava.lang.IllegalArgumentException.

22) E pop ();

  • From thisDequeRepresented by theStackAn element pops up on the stack. In other words, remove and return theDequeThe first element of.With this methodremoveFirst()Methods the equivalent;
  • If the stack is empty, it is thrownjava.util.NoSuchElementException.

The following is the Collection method

23) Boolean remove(Object o);

  • fromDequeRemoves the first match of the specified element from the If theDequeIf the element is not included in, it will not change. More formally, remove the first satisfaction(o==null ? e==null : o.equals(e))Element of an expressione(if such an element exists). If theDequeContains the specified element, removes it and returnstrue.With this methodremoveFirstOccurrence(Object o)Methods the equivalent;
  • If you specify the type of the element andDequeIf not compatible, throwjava.lang.ClassCastException(optional);
  • If the specified element isnullAnd thisDequeIt is not allowed tonullElement is thrownjava.lang.NullPointerException(Optional).

24) Boolean contains(Object O);

  • ifDequeReturns when containing the specified elementtrue. More formally, if and only if thisDequeContains at least one satisfaction(o==null ? e==null : o.equals(e))Element of an expressioneWhen to return totrue;
  • If you specify the type of the element andDequeIf not compatible, throwjava.lang.ClassCastException(optional);
  • If the specified element isnullAnd thisDequeIt is not allowed tonullElement is thrownjava.lang.NullPointerException(Optional).

25) public int size();

  • returnDequeThe number of elements contained in.

26) the Iterator < E > Iterator ();

  • Return in orderDequeAn iterator for all elements in the. The elements are returned in start-to-end order.

27) the Iterator < E > descendingIterator ();

  • Returns this in reverse orderDequeThe iterator for all elements in the. The elements are returned in end-to-end order.