Sub topic 7
Where the New Interfaces Sit in the Collections Hierarchy
This chapter assumes that you know List, Set, Map, Deque, Java interfaces, and the difference between ordered and unordered collections. The examples require Java 21 or newer.
Java 21 added three interfaces for collections and maps that have a defined encounter order:
SequencedCollection<E>SequencedSet<E>SequencedMap<K, V>
An encounter order is the order in which a collection returns its elements during iteration. These interfaces add common operations for the first element, the last element, and the reverse order.
The Collection Hierarchy
SequencedCollection extends Collection. Existing interfaces such as List and Deque now extend it. This means code can accept a SequencedCollection when it needs ordered access but does not need list indexes or queue-specific operations.
SequencedSet extends both Set and SequencedCollection. It describes a set with a defined encounter order and no duplicate elements.
Several existing interfaces were retrofitted into this hierarchy. Retrofitting means that Java changed an existing interface so that it extends a new interface. Existing classes can then provide the new operations without requiring a separate replacement class.
Common Collection Types
The main implementations fit into the hierarchy as follows:
| Type | Sequenced interface | Typical encounter order |
|---|---|---|
| `ArrayList` | `SequencedCollection` through `List` | Index order |
| `LinkedHashSet` | `SequencedSet` | Insertion order |
| `TreeSet` | `SequencedSet` through `SortedSet` | Comparator or natural order |
| `LinkedHashMap` | `SequencedMap` | Insertion or access order, depending on construction |
| `TreeMap` | `SequencedMap` through `SortedMap` | Key order |
HashSet and HashMap do not implement these interfaces because they do not promise an encounter order. Their current iteration order is not a contract that application code should depend on.
ArrayDeque implements Deque, and therefore also implements SequencedCollection. A deque already had operations such as addFirst and removeLast; the new hierarchy gives those operations a common type-level meaning across more collection classes.
Writing Methods Against the Interfaces
Use the narrowest interface that expresses what a method needs. If a method only needs to inspect the two ends, it can accept SequencedCollection instead of ArrayList or LinkedList.
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.SequencedCollection;
import java.util.SequencedSet;
public class HierarchyExample {
static <E> void printEnds(SequencedCollection<E> values) {
if (!values.isEmpty()) {
System.out.println("first: " + values.getFirst());
System.out.println("last: " + values.getLast());
}
}
static <E> void moveToFront(SequencedSet<E> values, E value) {
values.addFirst(value);
}
public static void main(String[] args) {
SequencedCollection<String> names =
new ArrayList<>(java.util.List.of("Ada", "Lin"));
SequencedSet<String> languages =
new LinkedHashSet<>(java.util.List.of("Java", "Kotlin"));
printEnds(names);
printEnds(languages);
moveToFront(languages, "Kotlin");
System.out.println(languages); // [Kotlin, Java]
}
}The printEnds method works with lists, deques, linked hash sets, and sorted sets. The moveToFront method requires a set because its argument must preserve uniqueness.
The Map Hierarchy Is Separate
Maps do not extend Collection, so SequencedMap is a separate branch. It extends Map and adds operations for entries at both ends, such as firstEntry, lastEntry, putFirst, and putLast.
LinkedHashMap and TreeMap implement SequencedMap through different ordering rules. A linked hash map normally follows its linked encounter order. A tree map follows its key comparator or natural key order. The interface is shared, but the meaning of “first” depends on the map's ordering contract.
The hierarchy also explains why some operations can be unsupported. A sorted collection determines positions from a comparator, so an operation that explicitly inserts at the front may not be meaningful. The class can still support getFirst and getLast while rejecting addFirst or addLast.
Migration Implications
When changing an existing method parameter to SequencedCollection, verify that every caller uses a Java 21-compatible type. Also check whether the code depends on list indexes, sorting, duplicate elements, or a specific map ordering rule. A broader interface improves reuse only when its ordering semantics match the method's assumptions.
Exercise
Which interface should a method accept if it needs first and last element operations, allows duplicates, and does not need indexes?
Show answer
Use SequencedCollection. It supports ordered end operations without requiring the caller to provide a List or a particular implementation.