Java / Java 21 Collection Framework features Interview questions
Why is Deque now classified as a SequencedCollection?
Deque already had exactly the semantics SequencedCollection was designed to generalize: elements at two distinguishable ends, first and last, with methods to add, read, and remove from either end.
Making Deque extend SequencedCollection meant the JDK could remove duplication rather than add it - methods like addFirst(), addLast(), getFirst(), getLast(), removeFirst(), and removeLast() were simply moved up to the shared interface instead of being redefined separately on Deque and again on the new interface.
Deque<String> deque = new ArrayDeque<>(); SequencedCollection<String> sc = deque; // valid - Deque IS-A SequencedCollection
This also means any method written to accept a SequencedCollection parameter automatically works with any Deque implementation passed in, without the caller needing a separate overload or an explicit type check.
More Related questions...