--- jsr166/src/main/java/util/Deque.java 2013/02/11 07:42:43 1.23 +++ jsr166/src/main/java/util/Deque.java 2018/10/01 00:10:53 1.42 @@ -27,41 +27,44 @@ package java.util; *

The twelve methods described above are summarized in the * following table: * - *

- * + *
+ * + * * - * - * - * + * + * + * * * - * - * - * - * - * + * + * + * + * * + * + * * - * - * - * - * - * + * + * + * + * + * * * - * - * - * - * - * + * + * + * + * + * * * - * - * - * - * - * + * + * + * + * + * * + * *
Summary of Deque methods
First Element (Head) Last Element (Tail) First Element (Head) Last Element (Tail)
Throws exceptionSpecial valueThrows exceptionSpecial valueThrows exceptionSpecial valueThrows exceptionSpecial value
Insert{@link #addFirst addFirst(e)}{@link #offerFirst offerFirst(e)}{@link #addLast addLast(e)}{@link #offerLast offerLast(e)}Insert{@link #addFirst(Object) addFirst(e)}{@link #offerFirst(Object) offerFirst(e)}{@link #addLast(Object) addLast(e)}{@link #offerLast(Object) offerLast(e)}
Remove{@link #removeFirst removeFirst()}{@link #pollFirst pollFirst()}{@link #removeLast removeLast()}{@link #pollLast pollLast()}Remove{@link #removeFirst() removeFirst()}{@link #pollFirst() pollFirst()}{@link #removeLast() removeLast()}{@link #pollLast() pollLast()}
Examine{@link #getFirst getFirst()}{@link #peekFirst peekFirst()}{@link #getLast getLast()}{@link #peekLast peekLast()}Examine{@link #getFirst() getFirst()}{@link #peekFirst() peekFirst()}{@link #getLast() getLast()}{@link #peekLast() peekLast()}
* *

This interface extends the {@link Queue} interface. When a deque is @@ -70,62 +73,70 @@ package java.util; * inherited from the {@code Queue} interface are precisely equivalent to * {@code Deque} methods as indicated in the following table: * - *

- * + *
+ * + * * - * - * + * + * * + * + * * - * - * + * + * * * - * - * + * + * * * - * - * + * + * * * - * - * + * + * * * - * - * + * + * * * - * - * + * + * * + * *
Comparison of Queue and Deque methods
{@code Queue} Method Equivalent {@code Deque} Method {@code Queue} Method Equivalent {@code Deque} Method
{@link java.util.Queue#add add(e)}{@link #addLast addLast(e)}{@link #add(Object) add(e)}{@link #addLast(Object) addLast(e)}
{@link java.util.Queue#offer offer(e)}{@link #offerLast offerLast(e)}{@link #offer(Object) offer(e)}{@link #offerLast(Object) offerLast(e)}
{@link java.util.Queue#remove remove()}{@link #removeFirst removeFirst()}{@link #remove() remove()}{@link #removeFirst() removeFirst()}
{@link java.util.Queue#poll poll()}{@link #pollFirst pollFirst()}{@link #poll() poll()}{@link #pollFirst() pollFirst()}
{@link java.util.Queue#element element()}{@link #getFirst getFirst()}{@link #element() element()}{@link #getFirst() getFirst()}
{@link java.util.Queue#peek peek()}{@link #peek peekFirst()}{@link #peek() peek()}{@link #peekFirst() peekFirst()}
* *

Deques can also be used as LIFO (Last-In-First-Out) stacks. This * interface should be used in preference to the legacy {@link Stack} class. * When a deque is used as a stack, elements are pushed and popped from the - * beginning of the deque. Stack methods are precisely equivalent to - * {@code Deque} methods as indicated in the table below: + * beginning of the deque. Stack methods are equivalent to {@code Deque} + * methods as indicated in the table below: * - *

- * + *
+ * + * * - * - * + * + * * + * + * * - * - * + * + * * * - * - * + * + * * * - * - * + * + * * + * *
Comparison of Stack and Deque methods
Stack Method Equivalent {@code Deque} Method Stack Method Equivalent {@code Deque} Method
{@link #push push(e)}{@link #addFirst addFirst(e)}{@link #push(Object) push(e)}{@link #addFirst(Object) addFirst(e)}
{@link #pop pop()}{@link #removeFirst removeFirst()}{@link #pop() pop()}{@link #removeFirst() removeFirst()}
{@link #peek peek()}{@link #peekFirst peekFirst()}{@link #peek() peek()}{@link #getFirst() getFirst()}
* *

Note that the {@link #peek peek} method works equally well when @@ -145,28 +156,29 @@ package java.util; * that do allow null elements are strongly encouraged not to * take advantage of the ability to insert nulls. This is so because * {@code null} is used as a special return value by various methods - * to indicated that the deque is empty. + * to indicate that the deque is empty. * *

{@code Deque} implementations generally do not define * element-based versions of the {@code equals} and {@code hashCode} * methods, but instead inherit the identity-based versions from class * {@code Object}. * - *

This interface is a member of the Java Collections - * Framework. + *

This interface is a member of the + * + * Java Collections Framework. * * @author Doug Lea * @author Josh Bloch * @since 1.6 - * @param the type of elements held in this collection + * @param the type of elements held in this deque */ public interface Deque extends Queue { /** * Inserts the specified element at the front of this deque if it is - * possible to do so immediately without violating capacity restrictions. - * When using a capacity-restricted deque, it is generally preferable to - * use method {@link #offerFirst}. + * possible to do so immediately without violating capacity restrictions, + * throwing an {@code IllegalStateException} if no space is currently + * available. When using a capacity-restricted deque, it is generally + * preferable to use method {@link #offerFirst}. * * @param e the element to add * @throws IllegalStateException if the element cannot be added at this @@ -182,9 +194,10 @@ public interface Deque extends Queue< /** * Inserts the specified element at the end of this deque if it is - * possible to do so immediately without violating capacity restrictions. - * When using a capacity-restricted deque, it is generally preferable to - * use method {@link #offerLast}. + * possible to do so immediately without violating capacity restrictions, + * throwing an {@code IllegalStateException} if no space is currently + * available. When using a capacity-restricted deque, it is generally + * preferable to use method {@link #offerLast}. * *

This method is equivalent to {@link #add}. * @@ -313,17 +326,18 @@ public interface Deque extends Queue< * Removes the first occurrence of the specified element from this deque. * If the deque does not contain the element, it is unchanged. * More formally, removes the first element {@code e} such that - * (o==null ? e==null : o.equals(e)) - * (if such an element exists). + * {@code Objects.equals(o, e)} (if such an element exists). * Returns {@code true} if this deque contained the specified element * (or equivalently, if this deque changed as a result of the call). * * @param o element to be removed from this deque, if present * @return {@code true} if an element was removed as a result of this call * @throws ClassCastException if the class of the specified element - * is incompatible with this deque (optional) + * is incompatible with this deque + * (optional) * @throws NullPointerException if the specified element is null and this - * deque does not permit null elements (optional) + * deque does not permit null elements + * (optional) */ boolean removeFirstOccurrence(Object o); @@ -331,17 +345,18 @@ public interface Deque extends Queue< * Removes the last occurrence of the specified element from this deque. * If the deque does not contain the element, it is unchanged. * More formally, removes the last element {@code e} such that - * (o==null ? e==null : o.equals(e)) - * (if such an element exists). + * {@code Objects.equals(o, e)} (if such an element exists). * Returns {@code true} if this deque contained the specified element * (or equivalently, if this deque changed as a result of the call). * * @param o element to be removed from this deque, if present * @return {@code true} if an element was removed as a result of this call * @throws ClassCastException if the class of the specified element - * is incompatible with this deque (optional) + * is incompatible with this deque + * (optional) * @throws NullPointerException if the specified element is null and this - * deque does not permit null elements (optional) + * deque does not permit null elements + * (optional) */ boolean removeLastOccurrence(Object o); @@ -397,8 +412,8 @@ public interface Deque extends Queue< /** * Retrieves and removes the head of the queue represented by this deque * (in other words, the first element of this deque). - * This method differs from {@link #poll poll} only in that it throws an - * exception if this deque is empty. + * This method differs from {@link #poll() poll()} only in that it + * throws an exception if this deque is empty. * *

This method is equivalent to {@link #removeFirst()}. * @@ -444,14 +459,38 @@ public interface Deque extends Queue< */ E peek(); + /** + * Adds all of the elements in the specified collection at the end + * of this deque, as if by calling {@link #addLast} on each one, + * in the order that they are returned by the collection's iterator. + * + *

When using a capacity-restricted deque, it is generally preferable + * to call {@link #offer(Object) offer} separately on each element. + * + *

An exception encountered while trying to add an element may result + * in only some of the elements having been successfully added when + * the associated exception is thrown. + * + * @param c the elements to be inserted into this deque + * @return {@code true} if this deque changed as a result of the call + * @throws IllegalStateException if not all the elements can be added at + * this time due to insertion restrictions + * @throws ClassCastException if the class of an element of the specified + * collection prevents it from being added to this deque + * @throws NullPointerException if the specified collection contains a + * null element and this deque does not permit null elements, + * or if the specified collection is null + * @throws IllegalArgumentException if some property of an element of the + * specified collection prevents it from being added to this deque + */ + boolean addAll(Collection c); // *** Stack methods *** /** * Pushes an element onto the stack represented by this deque (in other * words, at the head of this deque) if it is possible to do so - * immediately without violating capacity restrictions, returning - * {@code true} upon success and throwing an + * immediately without violating capacity restrictions, throwing an * {@code IllegalStateException} if no space is currently available. * *

This method is equivalent to {@link #addFirst}. @@ -487,8 +526,7 @@ public interface Deque extends Queue< * Removes the first occurrence of the specified element from this deque. * If the deque does not contain the element, it is unchanged. * More formally, removes the first element {@code e} such that - * (o==null ? e==null : o.equals(e)) - * (if such an element exists). + * {@code Objects.equals(o, e)} (if such an element exists). * Returns {@code true} if this deque contained the specified element * (or equivalently, if this deque changed as a result of the call). * @@ -497,24 +535,27 @@ public interface Deque extends Queue< * @param o element to be removed from this deque, if present * @return {@code true} if an element was removed as a result of this call * @throws ClassCastException if the class of the specified element - * is incompatible with this deque (optional) + * is incompatible with this deque + * (optional) * @throws NullPointerException if the specified element is null and this - * deque does not permit null elements (optional) + * deque does not permit null elements + * (optional) */ boolean remove(Object o); /** * Returns {@code true} if this deque contains the specified element. * More formally, returns {@code true} if and only if this deque contains - * at least one element {@code e} such that - * (o==null ? e==null : o.equals(e)). + * at least one element {@code e} such that {@code Objects.equals(o, e)}. * * @param o element whose presence in this deque is to be tested * @return {@code true} if this deque contains the specified element - * @throws ClassCastException if the type of the specified element - * is incompatible with this deque (optional) + * @throws ClassCastException if the class of the specified element + * is incompatible with this deque + * (optional) * @throws NullPointerException if the specified element is null and this - * deque does not permit null elements (optional) + * deque does not permit null elements + * (optional) */ boolean contains(Object o); @@ -523,7 +564,7 @@ public interface Deque extends Queue< * * @return the number of elements in this deque */ - public int size(); + int size(); /** * Returns an iterator over the elements in this deque in proper sequence.