Sequenced collections
Access first/last elements and reverse views with clean API methods.
Code Comparison
✕ Java 8
// Get last element var last = list.get(list.size() - 1); // Get first var first = list.get(0); // Reverse iteration: manual
✓ Java 21+
var last = list.getLast(); var first = list.getFirst(); var reversed = list.reversed();
Why the modern way wins
Self-documenting
getLast() is clearer than get(size()-1).
Reversed view
reversed() gives a view — no copying needed.
Uniform API
Works the same on List, Deque, SortedSet.
Old Approach
Index Arithmetic
Modern Approach
getFirst/getLast
Since JDK
21
Difficulty
beginner
JDK Support
Sequenced collections
Available
Widely available since JDK 21 LTS (Sept 2023)
How it works
SequencedCollection adds getFirst(), getLast(), reversed(), addFirst(), addLast() to List, Deque, SortedSet, and LinkedHashSet. No more size-1 arithmetic or manual reverse iteration.