Java / Java 21 Collection Framework features Interview questions
What is the SequencedCollection interface?
SequencedCollection<E> is a new interface in java.util that sits directly under Collection<E> in the hierarchy and represents any collection whose elements have a well-defined encounter order, from first to last.
It adds seven methods: addFirst(E), addLast(E), getFirst(), getLast(), removeFirst(), removeLast(), and reversed(). Most of these were promoted up from Deque, which already had first/last semantics.
SequencedCollection<String> names = new ArrayList<>(List.of("Ann", "Ben", "Cid")); names.addFirst("Aya"); System.out.println(names.getLast()); // Cid
If a concrete class doesn't logically support one of these operations (for example, adding to an immutable list), calling it throws UnsupportedOperationException instead of silently failing.
More Related questions...