One API for both ends
Java 21 added SequencedCollection, SequencedSet and SequencedMap: one standard way to get the first and last element, add at either end, and walk in reverse with reversed(), for every collection that has a defined order.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Using sequenced methods, write code that rotates a list one step to the left (first element moves to the end).
Print the three most recently inserted keys of a LinkedHashMap, newest first.
Explain it without notes
What problem do sequenced collections solve, and which Java version added them?
Why does TreeSet.addFirst throw, while LinkedHashSet.addFirst works?
Is list.reversed() a copy or a view? What are the consequences?
Expected output
[a, b, c, d] first=a last=d
reversed view: [d, c, b, a]
set: [3, 1, 2] last=2
map: {w=0, x=1, y=2} first=w=0 last=y=2
pollLastEntry: y=2 -> {w=0, x=1}