Path: blob/master/src/java.base/share/classes/java/util/Deque.java
41152 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* This file is available under and governed by the GNU General Public26* License version 2 only, as published by the Free Software Foundation.27* However, the following notice accompanied the original version of this28* file:29*30* Written by Doug Lea and Josh Bloch with assistance from members of31* JCP JSR-166 Expert Group and released to the public domain, as explained32* at http://creativecommons.org/publicdomain/zero/1.0/33*/3435package java.util;3637/**38* A linear collection that supports element insertion and removal at39* both ends. The name <i>deque</i> is short for "double ended queue"40* and is usually pronounced "deck". Most {@code Deque}41* implementations place no fixed limits on the number of elements42* they may contain, but this interface supports capacity-restricted43* deques as well as those with no fixed size limit.44*45* <p>This interface defines methods to access the elements at both46* ends of the deque. Methods are provided to insert, remove, and47* examine the element. Each of these methods exists in two forms:48* one throws an exception if the operation fails, the other returns a49* special value (either {@code null} or {@code false}, depending on50* the operation). The latter form of the insert operation is51* designed specifically for use with capacity-restricted52* {@code Deque} implementations; in most implementations, insert53* operations cannot fail.54*55* <p>The twelve methods described above are summarized in the56* following table:57*58* <table class="striped">59* <caption>Summary of Deque methods</caption>60* <thead>61* <tr>62* <td rowspan="2"></td>63* <th scope="col" colspan="2"> First Element (Head)</th>64* <th scope="col" colspan="2"> Last Element (Tail)</th>65* </tr>66* <tr>67* <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th>68* <th scope="col" style="font-weight:normal; font-style:italic">Special value</th>69* <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th>70* <th scope="col" style="font-weight:normal; font-style:italic">Special value</th>71* </tr>72* </thead>73* <tbody>74* <tr>75* <th scope="row">Insert</th>76* <td>{@link #addFirst(Object) addFirst(e)}</td>77* <td>{@link #offerFirst(Object) offerFirst(e)}</td>78* <td>{@link #addLast(Object) addLast(e)}</td>79* <td>{@link #offerLast(Object) offerLast(e)}</td>80* </tr>81* <tr>82* <th scope="row">Remove</th>83* <td>{@link #removeFirst() removeFirst()}</td>84* <td>{@link #pollFirst() pollFirst()}</td>85* <td>{@link #removeLast() removeLast()}</td>86* <td>{@link #pollLast() pollLast()}</td>87* </tr>88* <tr>89* <th scope="row">Examine</th>90* <td>{@link #getFirst() getFirst()}</td>91* <td>{@link #peekFirst() peekFirst()}</td>92* <td>{@link #getLast() getLast()}</td>93* <td>{@link #peekLast() peekLast()}</td>94* </tr>95* </tbody>96* </table>97*98* <p>This interface extends the {@link Queue} interface. When a deque is99* used as a queue, FIFO (First-In-First-Out) behavior results. Elements are100* added at the end of the deque and removed from the beginning. The methods101* inherited from the {@code Queue} interface are precisely equivalent to102* {@code Deque} methods as indicated in the following table:103*104* <table class="striped">105* <caption>Comparison of Queue and Deque methods</caption>106* <thead>107* <tr>108* <th scope="col"> {@code Queue} Method</th>109* <th scope="col"> Equivalent {@code Deque} Method</th>110* </tr>111* </thead>112* <tbody>113* <tr>114* <th scope="row">{@link #add(Object) add(e)}</th>115* <td>{@link #addLast(Object) addLast(e)}</td>116* </tr>117* <tr>118* <th scope="row">{@link #offer(Object) offer(e)}</th>119* <td>{@link #offerLast(Object) offerLast(e)}</td>120* </tr>121* <tr>122* <th scope="row">{@link #remove() remove()}</th>123* <td>{@link #removeFirst() removeFirst()}</td>124* </tr>125* <tr>126* <th scope="row">{@link #poll() poll()}</th>127* <td>{@link #pollFirst() pollFirst()}</td>128* </tr>129* <tr>130* <th scope="row">{@link #element() element()}</th>131* <td>{@link #getFirst() getFirst()}</td>132* </tr>133* <tr>134* <th scope="row">{@link #peek() peek()}</th>135* <td>{@link #peekFirst() peekFirst()}</td>136* </tr>137* </tbody>138* </table>139*140* <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This141* interface should be used in preference to the legacy {@link Stack} class.142* When a deque is used as a stack, elements are pushed and popped from the143* beginning of the deque. Stack methods are equivalent to {@code Deque}144* methods as indicated in the table below:145*146* <table class="striped">147* <caption>Comparison of Stack and Deque methods</caption>148* <thead>149* <tr>150* <th scope="col"> Stack Method</th>151* <th scope="col"> Equivalent {@code Deque} Method</th>152* </tr>153* </thead>154* <tbody>155* <tr>156* <th scope="row">{@link #push(Object) push(e)}</th>157* <td>{@link #addFirst(Object) addFirst(e)}</td>158* </tr>159* <tr>160* <th scope="row">{@link #pop() pop()}</th>161* <td>{@link #removeFirst() removeFirst()}</td>162* </tr>163* <tr>164* <th scope="row">{@link #peek() peek()}</th>165* <td>{@link #getFirst() getFirst()}</td>166* </tr>167* </tbody>168* </table>169*170* <p>Note that the {@link #peek peek} method works equally well when171* a deque is used as a queue or a stack; in either case, elements are172* drawn from the beginning of the deque.173*174* <p>This interface provides two methods to remove interior175* elements, {@link #removeFirstOccurrence removeFirstOccurrence} and176* {@link #removeLastOccurrence removeLastOccurrence}.177*178* <p>Unlike the {@link List} interface, this interface does not179* provide support for indexed access to elements.180*181* <p>While {@code Deque} implementations are not strictly required182* to prohibit the insertion of null elements, they are strongly183* encouraged to do so. Users of any {@code Deque} implementations184* that do allow null elements are strongly encouraged <i>not</i> to185* take advantage of the ability to insert nulls. This is so because186* {@code null} is used as a special return value by various methods187* to indicate that the deque is empty.188*189* <p>{@code Deque} implementations generally do not define190* element-based versions of the {@code equals} and {@code hashCode}191* methods, but instead inherit the identity-based versions from class192* {@code Object}.193*194* <p>This interface is a member of the195* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">196* Java Collections Framework</a>.197*198* @author Doug Lea199* @author Josh Bloch200* @since 1.6201* @param <E> the type of elements held in this deque202*/203public interface Deque<E> extends Queue<E> {204/**205* Inserts the specified element at the front of this deque if it is206* possible to do so immediately without violating capacity restrictions,207* throwing an {@code IllegalStateException} if no space is currently208* available. When using a capacity-restricted deque, it is generally209* preferable to use method {@link #offerFirst}.210*211* @param e the element to add212* @throws IllegalStateException if the element cannot be added at this213* time due to capacity restrictions214* @throws ClassCastException if the class of the specified element215* prevents it from being added to this deque216* @throws NullPointerException if the specified element is null and this217* deque does not permit null elements218* @throws IllegalArgumentException if some property of the specified219* element prevents it from being added to this deque220*/221void addFirst(E e);222223/**224* Inserts the specified element at the end of this deque if it is225* possible to do so immediately without violating capacity restrictions,226* throwing an {@code IllegalStateException} if no space is currently227* available. When using a capacity-restricted deque, it is generally228* preferable to use method {@link #offerLast}.229*230* <p>This method is equivalent to {@link #add}.231*232* @param e the element to add233* @throws IllegalStateException if the element cannot be added at this234* time due to capacity restrictions235* @throws ClassCastException if the class of the specified element236* prevents it from being added to this deque237* @throws NullPointerException if the specified element is null and this238* deque does not permit null elements239* @throws IllegalArgumentException if some property of the specified240* element prevents it from being added to this deque241*/242void addLast(E e);243244/**245* Inserts the specified element at the front of this deque unless it would246* violate capacity restrictions. When using a capacity-restricted deque,247* this method is generally preferable to the {@link #addFirst} method,248* which can fail to insert an element only by throwing an exception.249*250* @param e the element to add251* @return {@code true} if the element was added to this deque, else252* {@code false}253* @throws ClassCastException if the class of the specified element254* prevents it from being added to this deque255* @throws NullPointerException if the specified element is null and this256* deque does not permit null elements257* @throws IllegalArgumentException if some property of the specified258* element prevents it from being added to this deque259*/260boolean offerFirst(E e);261262/**263* Inserts the specified element at the end of this deque unless it would264* violate capacity restrictions. When using a capacity-restricted deque,265* this method is generally preferable to the {@link #addLast} method,266* which can fail to insert an element only by throwing an exception.267*268* @param e the element to add269* @return {@code true} if the element was added to this deque, else270* {@code false}271* @throws ClassCastException if the class of the specified element272* prevents it from being added to this deque273* @throws NullPointerException if the specified element is null and this274* deque does not permit null elements275* @throws IllegalArgumentException if some property of the specified276* element prevents it from being added to this deque277*/278boolean offerLast(E e);279280/**281* Retrieves and removes the first element of this deque. This method282* differs from {@link #pollFirst pollFirst} only in that it throws an283* exception if this deque is empty.284*285* @return the head of this deque286* @throws NoSuchElementException if this deque is empty287*/288E removeFirst();289290/**291* Retrieves and removes the last element of this deque. This method292* differs from {@link #pollLast pollLast} only in that it throws an293* exception if this deque is empty.294*295* @return the tail of this deque296* @throws NoSuchElementException if this deque is empty297*/298E removeLast();299300/**301* Retrieves and removes the first element of this deque,302* or returns {@code null} if this deque is empty.303*304* @return the head of this deque, or {@code null} if this deque is empty305*/306E pollFirst();307308/**309* Retrieves and removes the last element of this deque,310* or returns {@code null} if this deque is empty.311*312* @return the tail of this deque, or {@code null} if this deque is empty313*/314E pollLast();315316/**317* Retrieves, but does not remove, the first element of this deque.318*319* This method differs from {@link #peekFirst peekFirst} only in that it320* throws an exception if this deque is empty.321*322* @return the head of this deque323* @throws NoSuchElementException if this deque is empty324*/325E getFirst();326327/**328* Retrieves, but does not remove, the last element of this deque.329* This method differs from {@link #peekLast peekLast} only in that it330* throws an exception if this deque is empty.331*332* @return the tail of this deque333* @throws NoSuchElementException if this deque is empty334*/335E getLast();336337/**338* Retrieves, but does not remove, the first element of this deque,339* or returns {@code null} if this deque is empty.340*341* @return the head of this deque, or {@code null} if this deque is empty342*/343E peekFirst();344345/**346* Retrieves, but does not remove, the last element of this deque,347* or returns {@code null} if this deque is empty.348*349* @return the tail of this deque, or {@code null} if this deque is empty350*/351E peekLast();352353/**354* Removes the first occurrence of the specified element from this deque.355* If the deque does not contain the element, it is unchanged.356* More formally, removes the first element {@code e} such that357* {@code Objects.equals(o, e)} (if such an element exists).358* Returns {@code true} if this deque contained the specified element359* (or equivalently, if this deque changed as a result of the call).360*361* @param o element to be removed from this deque, if present362* @return {@code true} if an element was removed as a result of this call363* @throws ClassCastException if the class of the specified element364* is incompatible with this deque365* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)366* @throws NullPointerException if the specified element is null and this367* deque does not permit null elements368* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)369*/370boolean removeFirstOccurrence(Object o);371372/**373* Removes the last occurrence of the specified element from this deque.374* If the deque does not contain the element, it is unchanged.375* More formally, removes the last element {@code e} such that376* {@code Objects.equals(o, e)} (if such an element exists).377* Returns {@code true} if this deque contained the specified element378* (or equivalently, if this deque changed as a result of the call).379*380* @param o element to be removed from this deque, if present381* @return {@code true} if an element was removed as a result of this call382* @throws ClassCastException if the class of the specified element383* is incompatible with this deque384* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)385* @throws NullPointerException if the specified element is null and this386* deque does not permit null elements387* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)388*/389boolean removeLastOccurrence(Object o);390391// *** Queue methods ***392393/**394* Inserts the specified element into the queue represented by this deque395* (in other words, at the tail of this deque) if it is possible to do so396* immediately without violating capacity restrictions, returning397* {@code true} upon success and throwing an398* {@code IllegalStateException} if no space is currently available.399* When using a capacity-restricted deque, it is generally preferable to400* use {@link #offer(Object) offer}.401*402* <p>This method is equivalent to {@link #addLast}.403*404* @param e the element to add405* @return {@code true} (as specified by {@link Collection#add})406* @throws IllegalStateException if the element cannot be added at this407* time due to capacity restrictions408* @throws ClassCastException if the class of the specified element409* prevents it from being added to this deque410* @throws NullPointerException if the specified element is null and this411* deque does not permit null elements412* @throws IllegalArgumentException if some property of the specified413* element prevents it from being added to this deque414*/415boolean add(E e);416417/**418* Inserts the specified element into the queue represented by this deque419* (in other words, at the tail of this deque) if it is possible to do so420* immediately without violating capacity restrictions, returning421* {@code true} upon success and {@code false} if no space is currently422* available. When using a capacity-restricted deque, this method is423* generally preferable to the {@link #add} method, which can fail to424* insert an element only by throwing an exception.425*426* <p>This method is equivalent to {@link #offerLast}.427*428* @param e the element to add429* @return {@code true} if the element was added to this deque, else430* {@code false}431* @throws ClassCastException if the class of the specified element432* prevents it from being added to this deque433* @throws NullPointerException if the specified element is null and this434* deque does not permit null elements435* @throws IllegalArgumentException if some property of the specified436* element prevents it from being added to this deque437*/438boolean offer(E e);439440/**441* Retrieves and removes the head of the queue represented by this deque442* (in other words, the first element of this deque).443* This method differs from {@link #poll() poll()} only in that it444* throws an exception if this deque is empty.445*446* <p>This method is equivalent to {@link #removeFirst()}.447*448* @return the head of the queue represented by this deque449* @throws NoSuchElementException if this deque is empty450*/451E remove();452453/**454* Retrieves and removes the head of the queue represented by this deque455* (in other words, the first element of this deque), or returns456* {@code null} if this deque is empty.457*458* <p>This method is equivalent to {@link #pollFirst()}.459*460* @return the first element of this deque, or {@code null} if461* this deque is empty462*/463E poll();464465/**466* Retrieves, but does not remove, the head of the queue represented by467* this deque (in other words, the first element of this deque).468* This method differs from {@link #peek peek} only in that it throws an469* exception if this deque is empty.470*471* <p>This method is equivalent to {@link #getFirst()}.472*473* @return the head of the queue represented by this deque474* @throws NoSuchElementException if this deque is empty475*/476E element();477478/**479* Retrieves, but does not remove, the head of the queue represented by480* this deque (in other words, the first element of this deque), or481* returns {@code null} if this deque is empty.482*483* <p>This method is equivalent to {@link #peekFirst()}.484*485* @return the head of the queue represented by this deque, or486* {@code null} if this deque is empty487*/488E peek();489490/**491* Adds all of the elements in the specified collection at the end492* of this deque, as if by calling {@link #addLast} on each one,493* in the order that they are returned by the collection's iterator.494*495* <p>When using a capacity-restricted deque, it is generally preferable496* to call {@link #offer(Object) offer} separately on each element.497*498* <p>An exception encountered while trying to add an element may result499* in only some of the elements having been successfully added when500* the associated exception is thrown.501*502* @param c the elements to be inserted into this deque503* @return {@code true} if this deque changed as a result of the call504* @throws IllegalStateException if not all the elements can be added at505* this time due to insertion restrictions506* @throws ClassCastException if the class of an element of the specified507* collection prevents it from being added to this deque508* @throws NullPointerException if the specified collection contains a509* null element and this deque does not permit null elements,510* or if the specified collection is null511* @throws IllegalArgumentException if some property of an element of the512* specified collection prevents it from being added to this deque513*/514boolean addAll(Collection<? extends E> c);515516// *** Stack methods ***517518/**519* Pushes an element onto the stack represented by this deque (in other520* words, at the head of this deque) if it is possible to do so521* immediately without violating capacity restrictions, throwing an522* {@code IllegalStateException} if no space is currently available.523*524* <p>This method is equivalent to {@link #addFirst}.525*526* @param e the element to push527* @throws IllegalStateException if the element cannot be added at this528* time due to capacity restrictions529* @throws ClassCastException if the class of the specified element530* prevents it from being added to this deque531* @throws NullPointerException if the specified element is null and this532* deque does not permit null elements533* @throws IllegalArgumentException if some property of the specified534* element prevents it from being added to this deque535*/536void push(E e);537538/**539* Pops an element from the stack represented by this deque. In other540* words, removes and returns the first element of this deque.541*542* <p>This method is equivalent to {@link #removeFirst()}.543*544* @return the element at the front of this deque (which is the top545* of the stack represented by this deque)546* @throws NoSuchElementException if this deque is empty547*/548E pop();549550551// *** Collection methods ***552553/**554* Removes the first occurrence of the specified element from this deque.555* If the deque does not contain the element, it is unchanged.556* More formally, removes the first element {@code e} such that557* {@code Objects.equals(o, e)} (if such an element exists).558* Returns {@code true} if this deque contained the specified element559* (or equivalently, if this deque changed as a result of the call).560*561* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.562*563* @param o element to be removed from this deque, if present564* @return {@code true} if an element was removed as a result of this call565* @throws ClassCastException if the class of the specified element566* is incompatible with this deque567* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)568* @throws NullPointerException if the specified element is null and this569* deque does not permit null elements570* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)571*/572boolean remove(Object o);573574/**575* Returns {@code true} if this deque contains the specified element.576* More formally, returns {@code true} if and only if this deque contains577* at least one element {@code e} such that {@code Objects.equals(o, e)}.578*579* @param o element whose presence in this deque is to be tested580* @return {@code true} if this deque contains the specified element581* @throws ClassCastException if the class of the specified element582* is incompatible with this deque583* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)584* @throws NullPointerException if the specified element is null and this585* deque does not permit null elements586* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)587*/588boolean contains(Object o);589590/**591* Returns the number of elements in this deque.592*593* @return the number of elements in this deque594*/595int size();596597/**598* Returns an iterator over the elements in this deque in proper sequence.599* The elements will be returned in order from first (head) to last (tail).600*601* @return an iterator over the elements in this deque in proper sequence602*/603Iterator<E> iterator();604605/**606* Returns an iterator over the elements in this deque in reverse607* sequential order. The elements will be returned in order from608* last (tail) to first (head).609*610* @return an iterator over the elements in this deque in reverse611* sequence612*/613Iterator<E> descendingIterator();614615}616617618