Java / Java 21 Collection Framework features Interview questions
How does SequencedSet differ from a plain LinkedHashSet used before Java 21?
LinkedHashSet itself hasn't changed - it still maintains insertion order the same way it always has. What changed in Java 21 is that it now also implements SequencedSet, gaining a set of standard methods it didn't previously expose.
| Before Java 21 | Java 21 onward |
No getFirst()/getLast(); needed an iterator or stream to peek at ends | getFirst()/getLast() available directly |
Reversing required copying into a List and calling Collections.reverse() | reversed() gives a live reverse-order view directly |
| No standard way to add at a specific end | addFirst()/addLast() available |
So the difference isn't in how LinkedHashSet stores or orders its elements - it's in what API surface is now available for interacting with that existing order.
More Related questions...