Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

Java sequenced collections

Reference for Java 21 sequenced collections: getFirst(), getLast(), addFirst(), removeLast(), reversed() on lists, deques, LinkedHashSet and more.

Java 21 added three interfaces that describe collections with a defined encounter order: SequencedCollection<E>, SequencedSet<E> and SequencedMap<K,V>. Before them there was no common way to ask "what is the first element?" or "give me this in reverse" across List, Deque, LinkedHashSet, TreeSet and their map counterparts. Each had its own method names, or none at all.

SequencedCollection adds getFirst(), getLast(), addFirst(), addLast(), removeFirst(), removeLast() and reversed(). List and Deque now extend it, SequencedSet is implemented by LinkedHashSet and SortedSet/TreeSet, and SequencedMap by LinkedHashMap and SortedMap/TreeMap. HashSet and HashMap have no defined order and are not sequenced.

Java
List<String> list = new ArrayList<>(List.of("a", "b", "c"));
String first = list.getFirst();       // "a"
List<String> backwards = list.reversed();

Try it

Java First, last and reversed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java SequencedMap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public interface SequencedCollection<E> extends Collection<E> {
    SequencedCollection<E> reversed();
    default void addFirst(E e);
    default void addLast(E e);
    default E getFirst();
    default E getLast();
    default E removeFirst();
    default E removeLast();
}
Interface Implemented by Extra members
SequencedCollection<E> List, Deque The methods above.
SequencedSet<E> LinkedHashSet, SortedSet, TreeSet Covariant reversed() returning a SequencedSet.
SequencedMap<K,V> LinkedHashMap, SortedMap, TreeMap firstEntry(), lastEntry(), pollFirstEntry(), pollLastEntry(), putFirst(), putLast(), reversed(), sequencedKeySet(), sequencedValues(), sequencedEntrySet().

Throws: getFirst(), getLast(), removeFirst() and removeLast() throw NoSuchElementException on an empty collection. Methods that change a collection throw UnsupportedOperationException where unsupported (immutable collections, or sorted ones for addFirst/addLast/putFirst/putLast).

Usage notes

  • Since Java 21. Code that must run on older versions needs list.get(0), list.get(list.size() - 1) and Collections.reverse().
  • reversed() is a view. It reflects changes made to the original and vice versa. Copy it (new ArrayList<>(list.reversed())) for an independent reversed list.
  • Sorted collections order elements by comparator, so addFirst/addLast make no sense and throw. getFirst() and getLast() still work.
  • LinkedHashSet.addFirst(e) moves e to the front if it is already present. LinkedHashMap.putFirst and putLast also reposition existing keys.
  • Compare with Deque: Deque had getFirst/peekFirst before; the new List methods throw on an empty list, they never return null.
  • Streams over a reversed view read in reverse order, which gives a simple "last N" idiom.