Quick revision
The short answer
Before Java 8, adding a method to an interface broke every class that implemented it, so List, Deque and LinkedHashSet were frozen. Default methods made retrofitting possible, but not free: a new default method can clash with a method of the same name and a different return type in third-party implementations, so JEP 431 had to choose names and signatures carefully and accept some compatibility risk, because access to the first and last element was inconsistent across types. For our own long-lived APIs, the lessons are to add capabilities through new interfaces or default methods, test against known implementers before release, and get core abstractions right early, because retrofitting them years later is far more expensive.
What to remember
In this answer, we saw that ordered collections exposed their ends inconsistently for decades because, before Java 8, adding a method to an interface broke every implementation. Default methods made the retrofit possible but not free, because new methods can clash with same-named methods in third-party implementations, so JEP 431 had to check names and signatures and accept some risk in exchange for consistency. For our own APIs, we add capabilities through new interfaces or default methods, test against known implementers before release, and invest in core abstractions early, because retrofitting them later costs far more.
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.
Before Java 8, adding a method to an interface broke every class that implemented it, so List, Deque and LinkedHashSet were frozen. Default methods made retrofitting possible, but not free: a new default method can clash with a method of the same name and a different return type in third-party implementations, so JEP 431 had to choose names and signatures carefully and accept some compatibility risk, because access to the first and last element was inconsistent across types. For our own long-lived APIs, the lessons are to add capabilities through new interfaces or default methods, test against known implementers before release, and get core abstractions right early, because retrofitting them years later is far more expensive.
Why this question is asked
Sequenced collections look like a small convenience: getFirst and getLast on lists and sets. But the story of why they arrived only in Java 21, more than 25 years after the collections framework, is a lesson in API evolution that applies to any platform, internal library or public service contract. Interviewers ask senior engineers about it to see whether they understand why changing a widely implemented interface is hard, what tools make it possible, and what risk remains. In this answer, we will recap the inconsistency Java 21 fixed, see why it could not be fixed earlier, look at what default methods solve and what they do not, and then turn the experience into rules for our own APIs.
1A Quick Recap of the Inconsistency
Many collections have a defined encounter order, a first element and a last element, but before Java 21 each type exposed them differently. A List used get(0) and get(list.size() - 1). A Deque had getFirst() and getLast(). A SortedSet had first() and last(). A LinkedHashSet, although ordered, had no method for either end at all: the first element needed iterator().next(), and the last needed a full iteration. Reversing also differed: Deque had descendingIterator, NavigableSet had descendingSet, and List had nothing built in.
JEP 431 in Java 21 introduced SequencedCollection, SequencedSet and SequencedMap, with getFirst, getLast, addFirst, addLast, removeFirst, removeLast and reversed, and made List, Deque, LinkedHashSet, SortedSet, LinkedHashMap and SortedMap implement them.
| Type | Before Java 21 | Java 21 |
|---|---|---|
| List | list.get(list.size() - 1) | list.getLast() |
| Deque | deque.getLast() | deque.getLast() |
SortedSet | set.last() | set.getLast() |
LinkedHashSet | iterate the whole set | set.getLast() |
LinkedHashMap | iterate the entry set | map.lastEntry() |
2Why It Could Not Be Fixed Before Java 8
An interface in Java is a contract that implementing classes must fulfil. Before Java 8, interfaces could only declare abstract methods. Adding a method to List would have meant that every class implementing List, in the JDK, in libraries and in application code, no longer compiled, and already compiled classes would fail at runtime with AbstractMethodError when the new method was called. The JDK cannot break the world's code, so its central interfaces were effectively frozen.
Java 8 added default methods, interface methods with a body. A class that does not override the method inherits the default implementation, so adding a method no longer breaks implementers. Java 8 used this immediately for forEach, removeIf, stream and the Map methods such as computeIfAbsent and merge. So the tool existed from 2014, but sequenced collections still took until 2023.
3Why Default Methods Are Not Free
A default method is added to every implementing class at once, including classes the JDK team has never seen. If one of those classes already has a method with the same name and parameters, two things can happen. If the existing method has a compatible return type, it silently becomes an override, and its behaviour may not match the new contract. If it has an incompatible return type, the class no longer compiles.
So the JDK team had to examine the names it wanted to use against existing code, choose signatures that matched existing conventions such as Deque's getFirst, and accept that a small number of implementations would need changes. The JEP weighed that risk against the benefit of a consistent abstraction across the whole framework and judged the change worth it. There were also design questions, such as what reversed() should return for each type so that List.reversed() is a List and Deque.reversed() a Deque, which needed covariant return types in the new interfaces. Getting all of that right across a hierarchy that had grown for two decades took time, and that is a large part of why the abstraction arrived so late.
4Lessons for Our Own Long-Lived APIs
The first lesson is that the most expensive time to add an abstraction is after many teams have implemented the old one. If our internal library defines a Repository or Event interface used by fifty services, every method we forget now will be costly to add later. Spending design time on the core abstractions, and asking what operations every implementation will eventually need, is cheaper than a retrofit.
The second lesson is how to evolve safely when we must. We can add capabilities through new interfaces that existing types can opt into, as Java did with SequencedCollection, so old code keeps working unchanged. Or we can add default methods with careful names, and then test against known implementers before release: build the services that implement our interfaces against the new version in CI, so name and return type clashes appear in our pipeline rather than in their builds. For public APIs where we cannot see all implementers, we choose names that are unlikely to clash, document the new methods clearly and announce changes early.
The third lesson is to accept that some compatibility risk is the price of consistency, and to manage it openly. The JDK did not refuse the change because a few implementations might break; it measured the risk, chose to proceed and documented it. For our own platforms, that means a release note that names the possible clashes, and a migration path for the teams affected.
- Is the capability new to all implementers?YesConsider a new interface they opt intoNoConsider a default method
- Search known implementers for the same namebuild them against the new version in CI
- Any clash in return type or meaning?YesRename, or coordinate a migrationNoRelease with a clear note
- Document the default behaviour and when to override it
4 steps in order. Play them, or step through them one at a time.
5Follow-up questions
Why did SequencedCollection get a new interface instead of only new methods on Collection?
Because not every collection has an order. A HashSet has no first element, so getFirst on Collection would be meaningless or misleading. A separate interface expresses the concept precisely, and only types with a defined encounter order implement it.
Can default methods ever be removed again?
Practically no, once callers use them. Removing a method breaks every caller, which is worse than breaking implementers. That is another reason to design them carefully: adding is cautious but possible, removing is almost never possible.
How does this apply to service APIs rather than Java interfaces?
The same way. Adding an optional field to a response is like a default method: usually safe, but a client that already uses that field name for something else, or that rejects unknown fields, breaks. Contract tests with the main consumers play the role of building known implementers in CI.
6Conclusion
In this answer, we saw that ordered collections exposed their ends inconsistently for decades because, before Java 8, adding a method to an interface broke every implementation. Default methods made the retrofit possible but not free, because new methods can clash with same-named methods in third-party implementations, so JEP 431 had to check names and signatures and accept some risk in exchange for consistency. For our own APIs, we add capabilities through new interfaces or default methods, test against known implementers before release, and invest in core abstractions early, because retrofitting them later costs far more.
Open with
Before Java 21, every ordered collection had its own way to reach its ends: get zero and get size minus one on List, getFirst on Deque, first on SortedSet, and nothing at all on LinkedHashSet.
Then, if they want more
It took so long because before Java 8, adding any method to an interface like List broke every implementation, in the JDK and everywhere else, so the core interfaces were frozen. Default methods in Java 8 made retrofitting possible, but not free.
A new default method lands in every implementing class, and if a third-party class already has getFirst with a different return type, it stops compiling, or a same-named method silently becomes an override with the wrong meaning.
So JEP 431 had to choose names and signatures that matched existing conventions like Deque's, design covariant reversed methods across the hierarchy, and accept some compatibility risk because the inconsistency was worth fixing. For my own long-lived APIs, I take three lessons. Design the core abstraction carefully up front, because retrofitting is much more expensive.
When I must evolve it, add capabilities through new interfaces implementers can opt into, or default methods with careful names. And before release, build the known implementers against the new version in CI, and announce any possible clashes with a migration path.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 8
- 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 "Java 21 (JEP 431) retrofitted SequencedCollection, SequencedSet and SequencedMap onto...": https://scrivio-interview-library.netlify.app/java/sets/c444feb9cb/