APIdia Launch Partner
Top Description Methods
java.util

public Interface SequencedCollection<E>

extends Collection<E>
Known Direct Subinterfaces
java.util.SequencedSet, java.util.Deque, java.util.List
Known Direct Implementers
java.util.Collections.UnmodifiableSequencedCollection, java.util.LinkedHashMap.LinkedValues
Type Parameters
<E>
the type of elements in this collection

A collection that has a well-defined encounter order, that supports operations at both ends, and that is reversible. The elements of a sequenced collection have an encounter order, where conceptually the elements have a linear arrangement from the first element to the last element. Given any two elements, one element is either before (closer to the first element) or after (closer to the last element) the other element.

(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 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 equals and hashCode methods, because requirements imposed by sub-interfaces List and SequencedSet (which inherits requirements from Set) would be in conflict. See the specifications for Collection.equals and Collection.hashCode for further information.

Since
21

Method Summary

Modifier and TypeMethod and Description
public default void
addFirst(E
the element to be added
e
)

Adds an element as the first element of this collection (optional operation).

public default void
addLast(E
the element to be added.
e
)

Adds an element as the last element of this collection (optional operation).

public default E

Returns:

the retrieved element
getFirst
()

Gets the first element of this collection.

public default E

Returns:

the retrieved element
getLast
()

Gets the last element of this collection.

public default E

Returns:

the removed element
removeFirst
()

Removes and returns the first element of this collection (optional operation).

public default E

Returns:

the removed element
removeLast
()

Removes and returns the last element of this collection (optional operation).

public SequencedCollection<E>

Returns:

a reverse-ordered view of this collection
reversed
()

Returns a reverse-ordered view of this collection.

Inherited from java.util.Collection:
addaddAllclearcontainscontainsAllequalshashCodeisEmptyiteratorparallelStreamremoveremoveAllremoveIfretainAllsizespliteratorstreamtoArraytoArraytoArray

Method Detail

addFirstback to summary
public default void addFirst(E e)

Adds an element as the first element of this collection (optional operation). After this operation completes normally, the given element will be a member of this collection, and it will be the first element in encounter order.

Implementation Specification

The implementation in this interface always throws UnsupportedOperationException.

Parameters
e:E

the element to be added

Exceptions
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
addLastback to summary
public default void addLast(E e)

Adds an element as the last element of this collection (optional operation). After this operation completes normally, the given element will be a member of this collection, and it will be the last element in encounter order.

Implementation Specification

The implementation in this interface always throws UnsupportedOperationException.

Parameters
e:E

the element to be added.

Exceptions
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
getFirstback to summary
public default E getFirst()

Gets the first element of this collection.

Implementation Specification

The implementation in this interface obtains an iterator of this collection, and then it obtains an element by calling the iterator's next method. Any NoSuchElementException thrown is propagated. Otherwise, it returns the element.

Returns:E

the retrieved element

Exceptions
NoSuchElementException:
if this collection is empty
getLastback to summary
public default E getLast()

Gets the last element of this collection.

Implementation Specification

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 next method. Any NoSuchElementException thrown is propagated. Otherwise, it returns the element.

Returns:E

the retrieved element

Exceptions
NoSuchElementException:
if this collection is empty
removeFirstback to summary
public default E removeFirst()

Removes and returns the first element of this collection (optional operation).

Implementation Specification

The implementation in this interface obtains an iterator of this collection, and then it obtains an element by calling the iterator's next method. Any NoSuchElementException thrown is propagated. It then calls the iterator's remove method. Any UnsupportedOperationException thrown is propagated. Then, it returns the element.

Returns:E

the removed element

Exceptions
NoSuchElementException:
if this collection is empty
UnsupportedOperationException:
if this collection implementation does not support this operation
removeLastback to summary
public default E removeLast()

Removes and returns the last element of this collection (optional operation).

Implementation Specification

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 next method. Any NoSuchElementException thrown is propagated. It then calls the iterator's remove method. Any UnsupportedOperationException thrown is propagated. Then, it returns the element.

Returns:E

the removed element

Exceptions
NoSuchElementException:
if this collection is empty
UnsupportedOperationException:
if this collection implementation does not support this operation
reversedback to summary
public SequencedCollection<E> reversed()

Returns a reverse-ordered view of this collection. The encounter order of elements in the returned view is the inverse of the encounter order of elements in this collection. The reverse ordering affects all order-sensitive operations, including those on the view collections of the returned view. If the collection implementation permits modifications to this view, the modifications "write through" to the underlying collection. Changes to the underlying collection might or might not be visible in this reversed view, depending upon the implementation.

Returns:SequencedCollection<E>

a reverse-ordered view of this collection