Added in API level 35

SequencedCollection

public interface SequencedCollection
implements Collection<E>

java.util.SequencedCollection<E>
AbstractList<E> This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array). 
AbstractSequentialList<E> This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "sequential access" data store (such as a linked list). 
ArrayDeque<E> Resizable-array implementation of the Deque interface. 
ArrayList<E> Resizable-array implementation of the List interface. 
BlockingDeque<E> A Deque that additionally supports blocking operations that wait for the deque to become non-empty when retrieving an element, and wait for space to become available in the deque when storing an element. 
ConcurrentLinkedDeque<E> An unbounded concurrent deque based on linked nodes. 
ConcurrentSkipListSet<E> A scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap
CopyOnWriteArrayList<E> A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array. 
Deque<E> A linear collection that supports element insertion and removal at both ends. 
LinkedBlockingDeque<E> An optionally-bounded blocking deque based on linked nodes. 
LinkedHashSet<E>

Hash table and linked list implementation of the Set interface, with well-defined encounter order. 

LinkedList<E> Doubly-linked list implementation of the List and Deque interfaces. 
List<E> An ordered collection, where the user has precise control over where in the list each element is inserted. 
NavigableSet<E> A SortedSet extended with navigation methods reporting closest matches for given search targets. 
SequencedSet<E> A collection that is both a SequencedCollection and a Set
SortedSet<E> A Set that further provides a total ordering on its elements. 
Stack<E> The Stack class represents a last-in-first-out (LIFO) stack of objects. 
TreeSet<E> A NavigableSet implementation based on a TreeMap
Vector<E> The Vector class implements a growable array of objects. 


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 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.

Summary

Public methods

default void addFirst(E e)

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

default void addLast(E e)

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).

abstract SequencedCollection<E> reversed()

Returns a reverse-ordered view of this collection.

Inherited methods

abstract boolean add(E e)

Ensures that this collection contains the specified element (optional operation).

abstract boolean addAll(Collection<? extends E> c)

Adds all of the elements in the specified collection to this collection (optional operation).

abstract void clear()

Removes all of the elements from this collection (optional operation).

abstract boolean contains(Object o)

Returns true if this collection contains the specified element.

abstract boolean containsAll(Collection<?> c)

Returns true if this collection contains all of the elements in the specified collection.

abstract boolean equals(Object o)

Compares the specified object with this collection for equality.

abstract int hashCode()

Returns the hash code value for this collection.

abstract boolean isEmpty()

Returns true if this collection contains no elements.

abstract Iterator<E> iterator()

Returns an iterator over the elements in this collection.

default Stream<E> parallelStream()

Returns a possibly parallel Stream with this collection as its source.

abstract boolean remove(Object o)

Removes a single instance of the specified element from this collection, if it is present (optional operation).

abstract boolean removeAll(Collection<?> c)

Removes all of this collection's elements that are also contained in the specified collection (optional operation).

default boolean removeIf(Predicate<? super E> filter)

Removes all of the elements of this collection that satisfy the given predicate.

abstract boolean retainAll(Collection<?> c)

Retains only the elements in this collection that are contained in the specified collection (optional operation).

abstract int size()

Returns the number of elements in this collection.

default Spliterator<E> spliterator()

Creates a Spliterator over the elements in this collection.

default Stream<E> stream()

Returns a sequential Stream with this collection as its source.

abstract <T> T[] toArray(T[] a)

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

abstract Object[] toArray()

Returns an array containing all of the elements in this collection.

default <T> T[] toArray(IntFunction<T[]> generator)

Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.

default void forEach(Consumer<? super T> action)

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

abstract Iterator<E> iterator()

Returns an iterator over elements of type T.

default Spliterator<E> spliterator()

Creates a Spliterator over the elements described by this Iterable.

Public methods

addFirst

Added in API level 35
public 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 Requirements:
  • The implementation in this interface always throws UnsupportedOperationException.
Parameters
e 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

Added in API level 35
public 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 Requirements:
  • The implementation in this interface always throws UnsupportedOperationException.
Parameters
e 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

Added in API level 35
public E getFirst ()

Gets the first element of this collection.

Implementation Requirements:
  • 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

Throws
NoSuchElementException if this collection is empty

getLast

Added in API level 35
public E getLast ()

Gets the last element of this collection.

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

Throws
NoSuchElementException if this collection is empty

removeFirst

Added in API level 35
public E removeFirst ()

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

Implementation Requirements:
  • 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

Throws
NoSuchElementException if this collection is empty
UnsupportedOperationException if this collection implementation does not support this operation

removeLast

Added in API level 35
public E removeLast ()

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

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 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

Throws
NoSuchElementException if this collection is empty
UnsupportedOperationException if this collection implementation does not support this operation

reversed

Added in API level 35
public abstract 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