Sub topic 3
SequencedCollection: One API for Both Ends
Prerequisites and the problem it solves
Assume you know the core java.util collection interfaces, including List, Set, Map, and Deque. You should also know the difference between encounter order, sorted order, and an unordered collection. The examples require Java 21 or newer.
Before Java 21, collections used different methods for similar operations:
- A
Listusedget(0)for its first element andget(list.size() - 1)for its last element. - A
DequeusedgetFirst()andgetLast(). - A
LinkedHashSethad a defined order but no standard first and last operations. - Reversing a collection often required copying it or writing index-based code.
Java 21 introduced SequencedCollection. It gives collections with a defined encounter order one shared API for both ends.
Encounter order
Encounter order is the order in which a collection's iterator visits its elements. An ArrayList has encounter order because its elements have positions. A LinkedHashSet preserves insertion order. A TreeSet has comparator-defined sorted order.
A HashSet generally has no guaranteed encounter order, so it cannot provide the SequencedCollection contract.
The main operations are:
| Operation | Meaning |
|---|---|
| `getFirst()` | Read the first element |
| `getLast()` | Read the last element |
| `addFirst(e)` | Add an element at the beginning |
| `addLast(e)` | Add an element at the end |
| `removeFirst()` | Remove and return the first element |
| `removeLast()` | Remove and return the last element |
| `reversed()` | Return a reverse-order view |
addFirst, addLast, and the removal methods are optional operations. A particular collection can reject them with UnsupportedOperationException.
Using the common interface
You can declare a method with SequencedCollection when the method only needs ordered access to the ends. This makes the method work with several collection implementations.
import java.util.ArrayList;
import java.util.List;
import java.util.SequencedCollection;
public class ListEnds {
static void showEnds(SequencedCollection<String> values) {
System.out.println("first: " + values.getFirst());
System.out.println("last: " + values.getLast());
}
public static void main(String[] args) {
SequencedCollection<String> names =
new ArrayList<>(List.of("b", "c", "d"));
names.addFirst("a");
names.addLast("e");
showEnds(names);
System.out.println(names); // [a, b, c, d, e]
System.out.println(names.removeFirst()); // a
System.out.println(names.removeLast()); // e
System.out.println(names); // [b, c, d]
}
}The declared type controls which operations the code can use. The object is still an ArrayList, so its normal list behavior remains available when accessed through an ArrayList or List reference.
A Deque already has first-and-last operations. Java 21 aligns those operations with the SequencedCollection interface. This lets a method accept either a list or a deque when both provide the behavior the method needs.
Empty collections and optional operations
getFirst(), getLast(), removeFirst(), and removeLast() need an element to return. On an empty collection, they throw NoSuchElementException. Check isEmpty() first when emptiness is expected.
The interface does not force every implementation to support mutation. An unmodifiable sequenced collection can support reading and reversed() while rejecting addFirst, addLast, and removal operations. Code that accepts SequencedCollection should document whether it only reads or also modifies its argument.
Exercise
Which interface should a method accept if it needs to read the first and last elements of any ordered collection without depending on list indexes?
Show answer
It should accept SequencedCollection, using getFirst() and getLast().