Interface SequencedCollection<E>
- Type Parameters:
E- the type of elements in this collection
- All Superinterfaces:
Collection<E>, Iterable<E>
- All Known Subinterfaces:
BlockingDeque<E>, Deque<E>, List<E>, NavigableSet<E>, SequencedSet<E>, SortedSet<E>
- All Known Implementing Classes:
AbstractList, AbstractSequentialList, ArrayDeque, ArrayList, AttributeList, ConcurrentLinkedDeque, ConcurrentSkipListSet, CopyOnWriteArrayList, LinkedBlockingDeque, LinkedHashSet, LinkedList, RoleList, RoleUnresolvedList, Stack, TreeSet, Vector
public interface SequencedCollection<E> extends Collection<E>
(Note that this definition does not imply anything about physical positioning of elements, such as their locations in a computer's memory.)
Several methods inherited from the Collection interface are required to operate on elements according to this collection's encounter order. For instance, the iterator method provides elements starting from the first element, proceeding through successive elements, until the last element. Other methods that are required to operate on elements in encounter order include the following: forEach, parallelStream, spliterator, stream, and all overloads of the toArray method.
This interface provides methods to add, retrieve, and remove elements at either end of the collection.
This interface also defines the reversed method, which provides a reverse-ordered view of this collection. In the reverse-ordered view, the concepts of first and last are inverted, as are the concepts of successor and predecessor. The first element of this collection is the last element of the reverse-ordered view, and vice-versa. The successor of some element in this collection is its predecessor in the reversed view, and vice-versa. All methods that respect the encounter order of the collection operate as if the encounter order is inverted. For instance, the Collection.iterator() method of the reversed view reports the elements in order from the last element of this collection to the first. The availability of the reversed method, and its impact on the ordering semantics of all applicable methods, allow convenient iteration, searching, copying, and streaming of the elements of this collection in either forward order or reverse order.
This class is a member of the Java Collections Framework.
- API Note:
- This interface does not impose any requirements on the
equalsandhashCodemethods, because requirements imposed by sub-interfacesListandSequencedSet(which inherits requirements fromSet) would be in conflict. See the specifications forCollection.equalsandCollection.hashCodefor further information. - Since:
- 21
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
default void |
addFirst |
Adds an element as the first element of this collection (optional operation). |
default void |
addLast |
Adds an element as the last element of this collection (optional operation). |
default E |
getFirst() |
Gets the first element of this collection. |
default E |
getLast() |
Gets the last element of this collection. |
default E |
removeFirst() |
Removes and returns the first element of this collection (optional operation). |
default E |
removeLast() |
Removes and returns the last element of this collection (optional operation). |
SequencedCollection |
reversed() |
Returns a reverse-ordered view of this collection. |
Methods declared in interface Collection
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray | Modifier and Type | Method | Description |
|---|---|---|
boolean |
add |
Ensures that this collection contains the specified element (optional operation). |
boolean |
addAll |
Adds all of the elements in the specified collection to this collection (optional operation). |
void |
clear() |
Removes all of the elements from this collection (optional operation). |
boolean |
contains |
Returns true if this collection contains the specified element. |
boolean |
containsAll |
Returns true if this collection contains all of the elements in the specified collection. |
boolean |
equals |
Compares the specified object with this collection for equality. |
int |
hashCode() |
Returns the hash code value for this collection. |
boolean |
isEmpty() |
Returns true if this collection contains no elements. |
Iterator |
iterator() |
Returns an iterator over the elements in this collection. |
default Stream |
parallelStream() |
Returns a possibly parallel Stream with this collection as its source. |
boolean |
remove |
Removes a single instance of the specified element from this collection, if it is present (optional operation). |
boolean |
removeAll |
Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
default boolean |
removeIf |
Removes all of the elements of this collection that satisfy the given predicate (optional operation). |
boolean |
retainAll |
Retains only the elements in this collection that are contained in the specified collection (optional operation). |
int |
size() |
Returns the number of elements in this collection. |
default Spliterator |
spliterator() |
Creates a Spliterator over the elements in this collection. |
default Stream |
stream() |
Returns a sequential Stream with this collection as its source. |
Object[] |
toArray() |
Returns an array containing all of the elements in this collection. |
default <T> T[] |
toArray |
Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
<T> T[] |
toArray |
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
Method Details
reversed
SequencedCollection<E> reversed()
- Returns:
- a reverse-ordered view of this collection
addFirst
default void addFirst(E e)
- Implementation Requirements:
- The implementation in this interface always throws
UnsupportedOperationException. - Parameters:
-
e- the element to be added - Throws:
-
NullPointerException- if the specified element is null and this collection does not permit null elements -
UnsupportedOperationException- if this collection implementation does not support this operation
addLast
default void addLast(E e)
- Implementation Requirements:
- The implementation in this interface always throws
UnsupportedOperationException. - Parameters:
-
e- the element to be added. - Throws:
-
NullPointerException- if the specified element is null and this collection does not permit null elements -
UnsupportedOperationException- if this collection implementation does not support this operation
getFirst
default E getFirst()
- Implementation Requirements:
- The implementation in this interface obtains an iterator of this collection, and then it obtains an element by calling the iterator's
nextmethod. AnyNoSuchElementExceptionthrown is propagated. Otherwise, it returns the element. - Returns:
- the retrieved element
- Throws:
-
NoSuchElementException- if this collection is empty
getLast
default E getLast()
- Implementation Requirements:
- The implementation in this interface obtains an iterator of the reversed view of this collection, and then it obtains an element by calling the iterator's
nextmethod. AnyNoSuchElementExceptionthrown is propagated. Otherwise, it returns the element. - Returns:
- the retrieved element
- Throws:
-
NoSuchElementException- if this collection is empty
removeFirst
default E removeFirst()
- Implementation Requirements:
- The implementation in this interface obtains an iterator of this collection, and then it obtains an element by calling the iterator's
nextmethod. AnyNoSuchElementExceptionthrown is propagated. It then calls the iterator'sremovemethod. AnyUnsupportedOperationExceptionthrown is propagated. Then, it returns the element. - Returns:
- the removed element
- Throws:
-
NoSuchElementException- if this collection is empty -
UnsupportedOperationException- if this collection implementation does not support this operation
removeLast
default E removeLast()
- Implementation Requirements:
- The implementation in this interface obtains an iterator of the reversed view of this collection, and then it obtains an element by calling the iterator's
nextmethod. AnyNoSuchElementExceptionthrown is propagated. It then calls the iterator'sremovemethod. AnyUnsupportedOperationExceptionthrown is propagated. Then, it returns the element. - Returns:
- the removed element
- Throws:
-
NoSuchElementException- if this collection is empty -
UnsupportedOperationException- if this collection implementation does not support this operation
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.