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.
List<String> list = new ArrayList<>(List.of("a", "b", "c")); String first = list.getFirst(); // "a" List<String> backwards = list.reversed();
Try it
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.
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
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)andCollections.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/addLastmake no sense and throw.getFirst()andgetLast()still work. LinkedHashSet.addFirst(e)moveseto the front if it is already present.LinkedHashMap.putFirstandputLastalso reposition existing keys.- Compare with
Deque:DequehadgetFirst/peekFirstbefore; the newListmethods throw on an empty list, they never returnnull. - Streams over a reversed view read in reverse order, which gives a simple "last N" idiom.