Quick revision
The short answer
Since Java 21, every collection with a defined order shares the same methods: list.getFirst() and list.getLast(), set.getFirst() on a LinkedHashSet, map.firstEntry() and map.lastEntry() on a LinkedHashMap, and reversed(), which returns a reverse-ordered view we can loop over. Before Java 21 each type needed its own trick: list.get(0) and list.get(list.size() - 1), iterating a LinkedHashSet all the way to the end to find its last element, and a ListIterator walking backwards to reverse a list.
What to remember
In this answer, we saw that Java 21 gives every ordered collection the same methods: getFirst and getLast on lists and LinkedHashSets, firstEntry and lastEntry on LinkedHashMaps, and reversed() as a cheap view for reverse iteration. Before Java 21 we used list.get(0) and get(size - 1), walked a LinkedHashSet to its end and used a ListIterator to go backwards. We also saw the edge cases: getFirst throws NoSuchElementException on an empty collection, firstEntry returns null on an empty map, and the adding methods throw on unmodifiable collections.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Since Java 21, every collection with a defined order shares the same methods: list.getFirst() and list.getLast(), set.getFirst() on a LinkedHashSet, map.firstEntry() and map.lastEntry() on a LinkedHashMap, and reversed(), which returns a reverse-ordered view we can loop over. Before Java 21 each type needed its own trick: list.get(0) and list.get(list.size() - 1), iterating a LinkedHashSet all the way to the end to find its last element, and a ListIterator walking backwards to reverse a list.
Why this question is asked
Getting the first or last element sounds trivial, yet for years Java made us write it differently for every collection type, and some of those ways were slow or easy to get wrong. Java 21's sequenced collections, from JEP 431, fixed this with one set of methods, and interviewers ask about them because they are a recent change that every Java developer meets quickly after upgrading. In this answer, we will recap what a sequenced collection is, write the Java 21 code for a list, a LinkedHashSet and a LinkedHashMap, compare it with the code we needed before, and look at the edge cases: empty collections, adding at either end, and unmodifiable collections.
1A Quick Recap: What Sequenced Means
A collection is sequenced when its elements have a defined encounter order: a first element, a second, and so on up to a last. A List is sequenced by index. A LinkedHashSet is sequenced by insertion order. A TreeSet is sequenced by its sort order. A plain HashSet is not sequenced, because its iteration order depends on hash codes and can change as it grows.
Java 21 gave these collections a common type, SequencedCollection, with getFirst, getLast, addFirst, addLast, removeFirst, removeLast and reversed. SequencedSet does the same for ordered sets, and SequencedMap for ordered maps, with firstEntry, lastEntry, putFirst, putLast, pollFirstEntry, pollLastEntry and reversed. List, Deque, LinkedHashSet, SortedSet, LinkedHashMap and SortedMap all take part.
2First, Last and Reverse in Java 21
Here is the same task on all three types. Notice how the code now reads the same whatever the collection is.
reversed() is a view, not a copy. It costs nothing to create, and it reflects the original collection: if we add "d" to list, list.reversed().getFirst() returns "d". That makes it ideal for iteration in reverse without allocating a new list. If we need an independent reversed copy, we write new ArrayList<>(list.reversed()).
The map methods return entries, because a map's elements are key-value pairs. For only the keys or values in order, sequencedKeySet(), sequencedValues() and sequencedEntrySet() return sequenced views.
3What the Same Code Needed Before Java 21
Before Java 21, a List had index access, so first and last were easy but noisy. A LinkedHashSet had no method at all for its last element, so we had to walk through every element. The same was true for the last entry of a LinkedHashMap. Reverse iteration of a list needed a ListIterator started at the end.
| Task | Before Java 21 | Java 21 |
|---|---|---|
| First of a List | list.get(0) | list.getFirst() |
| Last of a List | list.get(list.size() - 1) | list.getLast() |
Last of a LinkedHashSet | iterate to the end, O(n) | set.getLast(), O(1) |
First entry of a LinkedHashMap | entrySet().iterator().next() | map.firstEntry() |
| Iterate a List backwards | ListIterator with hasPrevious() | for (x : list.reversed()) |
Notice the cost difference for the LinkedHashSet. Finding the last element used to take time proportional to the size of the set, because we had no way to reach its tail. LinkedHashSet always kept a pointer to its last node internally; Java 21 simply exposed it, so getLast() is now a constant-time call. Deque already had getFirst and getLast, which is where the new names came from.
4Edge Cases: Empty Collections
The new methods are strict about empty collections, and the collection and map versions differ. getFirst and getLast throw NoSuchElementException when the collection is empty. The old list.get(0) threw IndexOutOfBoundsException instead, so code that caught that exception needs to be updated when it moves to the new call. On a map, firstEntry and lastEntry return null when the map is empty, matching the older NavigableMap methods.
So we check isEmpty() first when emptiness is a normal case, and we let the exception propagate when an empty collection would mean a bug.
5Adding at Either End, and Unmodifiable Collections
The sequenced interfaces also add elements at either end. On an ArrayList, addFirst inserts at index 0, which shifts every element and costs O(n); on an ArrayDeque or LinkedList it is cheap. On a LinkedHashSet, addFirst puts the element at the front, and if the element is already present it is moved there. LinkedHashMap has putFirst and putLast with the same moving behaviour for an existing key.
Notice that reading works on every sequenced collection, but the adding and removing methods are optional operations. An unmodifiable collection such as List.of throws UnsupportedOperationException, and a sorted collection such as TreeSet throws for addFirst and addLast, because the position of an element is decided by its sort order, not by us.
6Follow-up questions
Does a HashSet or HashMap have getFirst?
No. They have no defined encounter order, so they do not implement SequencedSet or SequencedMap. Calling getFirst on a HashSet does not compile. If we need first and last, we choose LinkedHashSet, LinkedHashMap, or a sorted collection.
Is list.reversed() safe to modify?
For a modifiable list, yes: changes through the view go to the original list in mirrored positions, so adding to the end of the reversed view adds to the front of the original. For an unmodifiable list, the view is unmodifiable too. Most of the time we use it only for reading.
Can I write a method that accepts any sequenced collection?
Yes, that is one of the main benefits. A parameter of type SequencedCollection<T> accepts a List, a Deque, a LinkedHashSet or a TreeSet, and the method can call getFirst, getLast and reversed on all of them. Before Java 21, there was no such common type.
7Conclusion
In this answer, we saw that Java 21 gives every ordered collection the same methods: getFirst and getLast on lists and LinkedHashSets, firstEntry and lastEntry on LinkedHashMaps, and reversed() as a cheap view for reverse iteration. Before Java 21 we used list.get(0) and get(size - 1), walked a LinkedHashSet to its end and used a ListIterator to go backwards. We also saw the edge cases: getFirst throws NoSuchElementException on an empty collection, firstEntry returns null on an empty map, and the adding methods throw on unmodifiable collections.
Open with
In Java 21, sequenced collections give all ordered collections the same API. For a List I call getFirst and getLast. For a LinkedHashSet it is the same, set.getFirst and set.getLast, and for a LinkedHashMap I use firstEntry and lastEntry, or sequencedKeySet if I only need the keys.
Then, if they want more
To iterate in reverse, I call reversed, which returns a view, so it does not copy anything and reflects later changes. Before Java 21, every type was different. For a list, I wrote list.get of zero and list.get of size minus one.
For a LinkedHashSet there was no last method at all, so I had to iterate through every element to reach the end, which is linear time, while getLast is now constant time. For a map I took entrySet's iterator and called next. To go backwards through a list I created a ListIterator at the end and looped with hasPrevious.
There are a few edge cases I would mention. getFirst on an empty collection throws NoSuchElementException, while the old get of zero threw IndexOutOfBoundsException, and firstEntry on an empty map returns null.
There are also addFirst and addLast, and putFirst on LinkedHashMap, but they are optional operations, so List.of throws UnsupportedOperationException, and on an ArrayList addFirst is linear because it shifts elements.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Using Java 21 sequenced collections, get the first and last elements of a List, a...": https://scrivio-interview-library.netlify.app/java/collections-overview/3f61a82457/