Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/util/Deque.java
38829 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 BORDER CELLPADDING=3 CELLSPACING=1>59* <caption>Summary of Deque methods</caption>60* <tr>61* <td></td>62* <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td>63* <td ALIGN=CENTER COLSPAN = 2> <b>Last Element (Tail)</b></td>64* </tr>65* <tr>66* <td></td>67* <td ALIGN=CENTER><em>Throws exception</em></td>68* <td ALIGN=CENTER><em>Special value</em></td>69* <td ALIGN=CENTER><em>Throws exception</em></td>70* <td ALIGN=CENTER><em>Special value</em></td>71* </tr>72* <tr>73* <td><b>Insert</b></td>74* <td>{@link Deque#addFirst addFirst(e)}</td>75* <td>{@link Deque#offerFirst offerFirst(e)}</td>76* <td>{@link Deque#addLast addLast(e)}</td>77* <td>{@link Deque#offerLast offerLast(e)}</td>78* </tr>79* <tr>80* <td><b>Remove</b></td>81* <td>{@link Deque#removeFirst removeFirst()}</td>82* <td>{@link Deque#pollFirst pollFirst()}</td>83* <td>{@link Deque#removeLast removeLast()}</td>84* <td>{@link Deque#pollLast pollLast()}</td>85* </tr>86* <tr>87* <td><b>Examine</b></td>88* <td>{@link Deque#getFirst getFirst()}</td>89* <td>{@link Deque#peekFirst peekFirst()}</td>90* <td>{@link Deque#getLast getLast()}</td>91* <td>{@link Deque#peekLast peekLast()}</td>92* </tr>93* </table>94*95* <p>This interface extends the {@link Queue} interface. When a deque is96* used as a queue, FIFO (First-In-First-Out) behavior results. Elements are97* added at the end of the deque and removed from the beginning. The methods98* inherited from the {@code Queue} interface are precisely equivalent to99* {@code Deque} methods as indicated in the following table:100*101* <table BORDER CELLPADDING=3 CELLSPACING=1>102* <caption>Comparison of Queue and Deque methods</caption>103* <tr>104* <td ALIGN=CENTER> <b>{@code Queue} Method</b></td>105* <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td>106* </tr>107* <tr>108* <td>{@link java.util.Queue#add add(e)}</td>109* <td>{@link #addLast addLast(e)}</td>110* </tr>111* <tr>112* <td>{@link java.util.Queue#offer offer(e)}</td>113* <td>{@link #offerLast offerLast(e)}</td>114* </tr>115* <tr>116* <td>{@link java.util.Queue#remove remove()}</td>117* <td>{@link #removeFirst removeFirst()}</td>118* </tr>119* <tr>120* <td>{@link java.util.Queue#poll poll()}</td>121* <td>{@link #pollFirst pollFirst()}</td>122* </tr>123* <tr>124* <td>{@link java.util.Queue#element element()}</td>125* <td>{@link #getFirst getFirst()}</td>126* </tr>127* <tr>128* <td>{@link java.util.Queue#peek peek()}</td>129* <td>{@link #peek peekFirst()}</td>130* </tr>131* </table>132*133* <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This134* interface should be used in preference to the legacy {@link Stack} class.135* When a deque is used as a stack, elements are pushed and popped from the136* beginning of the deque. Stack methods are precisely equivalent to137* {@code Deque} methods as indicated in the table below:138*139* <table BORDER CELLPADDING=3 CELLSPACING=1>140* <caption>Comparison of Stack and Deque methods</caption>141* <tr>142* <td ALIGN=CENTER> <b>Stack Method</b></td>143* <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td>144* </tr>145* <tr>146* <td>{@link #push push(e)}</td>147* <td>{@link #addFirst addFirst(e)}</td>148* </tr>149* <tr>150* <td>{@link #pop pop()}</td>151* <td>{@link #removeFirst removeFirst()}</td>152* </tr>153* <tr>154* <td>{@link #peek peek()}</td>155* <td>{@link #peekFirst peekFirst()}</td>156* </tr>157* </table>158*159* <p>Note that the {@link #peek peek} method works equally well when160* a deque is used as a queue or a stack; in either case, elements are161* drawn from the beginning of the deque.162*163* <p>This interface provides two methods to remove interior164* elements, {@link #removeFirstOccurrence removeFirstOccurrence} and165* {@link #removeLastOccurrence removeLastOccurrence}.166*167* <p>Unlike the {@link List} interface, this interface does not168* provide support for indexed access to elements.169*170* <p>While {@code Deque} implementations are not strictly required171* to prohibit the insertion of null elements, they are strongly172* encouraged to do so. Users of any {@code Deque} implementations173* that do allow null elements are strongly encouraged <i>not</i> to174* take advantage of the ability to insert nulls. This is so because175* {@code null} is used as a special return value by various methods176* to indicated that the deque is empty.177*178* <p>{@code Deque} implementations generally do not define179* element-based versions of the {@code equals} and {@code hashCode}180* methods, but instead inherit the identity-based versions from class181* {@code Object}.182*183* <p>This interface is a member of the <a184* href="{@docRoot}/../technotes/guides/collections/index.html"> Java Collections185* Framework</a>.186*187* @author Doug Lea188* @author Josh Bloch189* @since 1.6190* @param <E> the type of elements held in this collection191*/192public interface Deque<E> extends Queue<E> {193/**194* Inserts the specified element at the front of this deque if it is195* possible to do so immediately without violating capacity restrictions,196* throwing an {@code IllegalStateException} if no space is currently197* available. When using a capacity-restricted deque, it is generally198* preferable to use method {@link #offerFirst}.199*200* @param e the element to add201* @throws IllegalStateException if the element cannot be added at this202* time due to capacity restrictions203* @throws ClassCastException if the class of the specified element204* prevents it from being added to this deque205* @throws NullPointerException if the specified element is null and this206* deque does not permit null elements207* @throws IllegalArgumentException if some property of the specified208* element prevents it from being added to this deque209*/210void addFirst(E e);211212/**213* Inserts the specified element at the end of this deque if it is214* possible to do so immediately without violating capacity restrictions,215* throwing an {@code IllegalStateException} if no space is currently216* available. When using a capacity-restricted deque, it is generally217* preferable to use method {@link #offerLast}.218*219* <p>This method is equivalent to {@link #add}.220*221* @param e the element to add222* @throws IllegalStateException if the element cannot be added at this223* time due to capacity restrictions224* @throws ClassCastException if the class of the specified element225* prevents it from being added to this deque226* @throws NullPointerException if the specified element is null and this227* deque does not permit null elements228* @throws IllegalArgumentException if some property of the specified229* element prevents it from being added to this deque230*/231void addLast(E e);232233/**234* Inserts the specified element at the front of this deque unless it would235* violate capacity restrictions. When using a capacity-restricted deque,236* this method is generally preferable to the {@link #addFirst} method,237* which can fail to insert an element only by throwing an exception.238*239* @param e the element to add240* @return {@code true} if the element was added to this deque, else241* {@code false}242* @throws ClassCastException if the class of the specified element243* prevents it from being added to this deque244* @throws NullPointerException if the specified element is null and this245* deque does not permit null elements246* @throws IllegalArgumentException if some property of the specified247* element prevents it from being added to this deque248*/249boolean offerFirst(E e);250251/**252* Inserts the specified element at the end of this deque unless it would253* violate capacity restrictions. When using a capacity-restricted deque,254* this method is generally preferable to the {@link #addLast} method,255* which can fail to insert an element only by throwing an exception.256*257* @param e the element to add258* @return {@code true} if the element was added to this deque, else259* {@code false}260* @throws ClassCastException if the class of the specified element261* prevents it from being added to this deque262* @throws NullPointerException if the specified element is null and this263* deque does not permit null elements264* @throws IllegalArgumentException if some property of the specified265* element prevents it from being added to this deque266*/267boolean offerLast(E e);268269/**270* Retrieves and removes the first element of this deque. This method271* differs from {@link #pollFirst pollFirst} only in that it throws an272* exception if this deque is empty.273*274* @return the head of this deque275* @throws NoSuchElementException if this deque is empty276*/277E removeFirst();278279/**280* Retrieves and removes the last element of this deque. This method281* differs from {@link #pollLast pollLast} only in that it throws an282* exception if this deque is empty.283*284* @return the tail of this deque285* @throws NoSuchElementException if this deque is empty286*/287E removeLast();288289/**290* Retrieves and removes the first element of this deque,291* or returns {@code null} if this deque is empty.292*293* @return the head of this deque, or {@code null} if this deque is empty294*/295E pollFirst();296297/**298* Retrieves and removes the last element of this deque,299* or returns {@code null} if this deque is empty.300*301* @return the tail of this deque, or {@code null} if this deque is empty302*/303E pollLast();304305/**306* Retrieves, but does not remove, the first element of this deque.307*308* This method differs from {@link #peekFirst peekFirst} only in that it309* throws an exception if this deque is empty.310*311* @return the head of this deque312* @throws NoSuchElementException if this deque is empty313*/314E getFirst();315316/**317* Retrieves, but does not remove, the last element of this deque.318* This method differs from {@link #peekLast peekLast} only in that it319* throws an exception if this deque is empty.320*321* @return the tail of this deque322* @throws NoSuchElementException if this deque is empty323*/324E getLast();325326/**327* Retrieves, but does not remove, the first element of this deque,328* or returns {@code null} if this deque is empty.329*330* @return the head of this deque, or {@code null} if this deque is empty331*/332E peekFirst();333334/**335* Retrieves, but does not remove, the last element of this deque,336* or returns {@code null} if this deque is empty.337*338* @return the tail of this deque, or {@code null} if this deque is empty339*/340E peekLast();341342/**343* Removes the first occurrence of the specified element from this deque.344* If the deque does not contain the element, it is unchanged.345* More formally, removes the first element {@code e} such that346* <tt>(o==null ? e==null : o.equals(e))</tt>347* (if such an element exists).348* Returns {@code true} if this deque contained the specified element349* (or equivalently, if this deque changed as a result of the call).350*351* @param o element to be removed from this deque, if present352* @return {@code true} if an element was removed as a result of this call353* @throws ClassCastException if the class of the specified element354* is incompatible with this deque355* (<a href="Collection.html#optional-restrictions">optional</a>)356* @throws NullPointerException if the specified element is null and this357* deque does not permit null elements358* (<a href="Collection.html#optional-restrictions">optional</a>)359*/360boolean removeFirstOccurrence(Object o);361362/**363* Removes the last occurrence of the specified element from this deque.364* If the deque does not contain the element, it is unchanged.365* More formally, removes the last element {@code e} such that366* <tt>(o==null ? e==null : o.equals(e))</tt>367* (if such an element exists).368* Returns {@code true} if this deque contained the specified element369* (or equivalently, if this deque changed as a result of the call).370*371* @param o element to be removed from this deque, if present372* @return {@code true} if an element was removed as a result of this call373* @throws ClassCastException if the class of the specified element374* is incompatible with this deque375* (<a href="Collection.html#optional-restrictions">optional</a>)376* @throws NullPointerException if the specified element is null and this377* deque does not permit null elements378* (<a href="Collection.html#optional-restrictions">optional</a>)379*/380boolean removeLastOccurrence(Object o);381382// *** Queue methods ***383384/**385* Inserts the specified element into the queue represented by this deque386* (in other words, at the tail of this deque) if it is possible to do so387* immediately without violating capacity restrictions, returning388* {@code true} upon success and throwing an389* {@code IllegalStateException} if no space is currently available.390* When using a capacity-restricted deque, it is generally preferable to391* use {@link #offer(Object) offer}.392*393* <p>This method is equivalent to {@link #addLast}.394*395* @param e the element to add396* @return {@code true} (as specified by {@link Collection#add})397* @throws IllegalStateException if the element cannot be added at this398* time due to capacity restrictions399* @throws ClassCastException if the class of the specified element400* prevents it from being added to this deque401* @throws NullPointerException if the specified element is null and this402* deque does not permit null elements403* @throws IllegalArgumentException if some property of the specified404* element prevents it from being added to this deque405*/406boolean add(E e);407408/**409* Inserts the specified element into the queue represented by this deque410* (in other words, at the tail of this deque) if it is possible to do so411* immediately without violating capacity restrictions, returning412* {@code true} upon success and {@code false} if no space is currently413* available. When using a capacity-restricted deque, this method is414* generally preferable to the {@link #add} method, which can fail to415* insert an element only by throwing an exception.416*417* <p>This method is equivalent to {@link #offerLast}.418*419* @param e the element to add420* @return {@code true} if the element was added to this deque, else421* {@code false}422* @throws ClassCastException if the class of the specified element423* prevents it from being added to this deque424* @throws NullPointerException if the specified element is null and this425* deque does not permit null elements426* @throws IllegalArgumentException if some property of the specified427* element prevents it from being added to this deque428*/429boolean offer(E e);430431/**432* Retrieves and removes the head of the queue represented by this deque433* (in other words, the first element of this deque).434* This method differs from {@link #poll poll} only in that it throws an435* exception if this deque is empty.436*437* <p>This method is equivalent to {@link #removeFirst()}.438*439* @return the head of the queue represented by this deque440* @throws NoSuchElementException if this deque is empty441*/442E remove();443444/**445* Retrieves and removes the head of the queue represented by this deque446* (in other words, the first element of this deque), or returns447* {@code null} if this deque is empty.448*449* <p>This method is equivalent to {@link #pollFirst()}.450*451* @return the first element of this deque, or {@code null} if452* this deque is empty453*/454E poll();455456/**457* Retrieves, but does not remove, the head of the queue represented by458* this deque (in other words, the first element of this deque).459* This method differs from {@link #peek peek} only in that it throws an460* exception if this deque is empty.461*462* <p>This method is equivalent to {@link #getFirst()}.463*464* @return the head of the queue represented by this deque465* @throws NoSuchElementException if this deque is empty466*/467E element();468469/**470* Retrieves, but does not remove, the head of the queue represented by471* this deque (in other words, the first element of this deque), or472* returns {@code null} if this deque is empty.473*474* <p>This method is equivalent to {@link #peekFirst()}.475*476* @return the head of the queue represented by this deque, or477* {@code null} if this deque is empty478*/479E peek();480481482// *** Stack methods ***483484/**485* Pushes an element onto the stack represented by this deque (in other486* words, at the head of this deque) if it is possible to do so487* immediately without violating capacity restrictions, throwing an488* {@code IllegalStateException} if no space is currently available.489*490* <p>This method is equivalent to {@link #addFirst}.491*492* @param e the element to push493* @throws IllegalStateException if the element cannot be added at this494* time due to capacity restrictions495* @throws ClassCastException if the class of the specified element496* prevents it from being added to this deque497* @throws NullPointerException if the specified element is null and this498* deque does not permit null elements499* @throws IllegalArgumentException if some property of the specified500* element prevents it from being added to this deque501*/502void push(E e);503504/**505* Pops an element from the stack represented by this deque. In other506* words, removes and returns the first element of this deque.507*508* <p>This method is equivalent to {@link #removeFirst()}.509*510* @return the element at the front of this deque (which is the top511* of the stack represented by this deque)512* @throws NoSuchElementException if this deque is empty513*/514E pop();515516517// *** Collection methods ***518519/**520* Removes the first occurrence of the specified element from this deque.521* If the deque does not contain the element, it is unchanged.522* More formally, removes the first element {@code e} such that523* <tt>(o==null ? e==null : o.equals(e))</tt>524* (if such an element exists).525* Returns {@code true} if this deque contained the specified element526* (or equivalently, if this deque changed as a result of the call).527*528* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.529*530* @param o element to be removed from this deque, if present531* @return {@code true} if an element was removed as a result of this call532* @throws ClassCastException if the class of the specified element533* is incompatible with this deque534* (<a href="Collection.html#optional-restrictions">optional</a>)535* @throws NullPointerException if the specified element is null and this536* deque does not permit null elements537* (<a href="Collection.html#optional-restrictions">optional</a>)538*/539boolean remove(Object o);540541/**542* Returns {@code true} if this deque contains the specified element.543* More formally, returns {@code true} if and only if this deque contains544* at least one element {@code e} such that545* <tt>(o==null ? e==null : o.equals(e))</tt>.546*547* @param o element whose presence in this deque is to be tested548* @return {@code true} if this deque contains the specified element549* @throws ClassCastException if the type of the specified element550* is incompatible with this deque551* (<a href="Collection.html#optional-restrictions">optional</a>)552* @throws NullPointerException if the specified element is null and this553* deque does not permit null elements554* (<a href="Collection.html#optional-restrictions">optional</a>)555*/556boolean contains(Object o);557558/**559* Returns the number of elements in this deque.560*561* @return the number of elements in this deque562*/563public int size();564565/**566* Returns an iterator over the elements in this deque in proper sequence.567* The elements will be returned in order from first (head) to last (tail).568*569* @return an iterator over the elements in this deque in proper sequence570*/571Iterator<E> iterator();572573/**574* Returns an iterator over the elements in this deque in reverse575* sequential order. The elements will be returned in order from576* last (tail) to first (head).577*578* @return an iterator over the elements in this deque in reverse579* sequence580*/581Iterator<E> descendingIterator();582583}584585586