- Published on
Java Sequenced Collections
- Authors
- Name
- Douglas Montanus
JEP 431: Sequenced Collections
Status: Delivered in Java 21
Author: Stuart Marks
Reviewed by: Brian Goetz
Component: java.util:collections
🧭 Summary
JEP 431 introduces SequencedCollection, SequencedSet, and SequencedMap — new interfaces that define a consistent, reversible encounter order for Java collections. These interfaces unify how elements are accessed from the front or back and allow bidirectional iteration across all collection types that maintain order.
💡 Motivation
Java lacked a unified abstraction for collections with a defined order. Although classes like List
, Deque
, LinkedHashSet
, and SortedSet
implied order, there was:
- No shared interface that declared operations like
getFirst()
,getLast()
, orreversed()
- Inconsistent APIs across types for accessing first/last elements
- No reverse stream support without copying or complex logic
🧱 New Interfaces
SequencedCollection<E>
A base interface for collections with defined order.
interface SequencedCollection<E> extends Collection<E> {
SequencedCollection<E> reversed();
void addFirst(E);
void addLast(E);
E getFirst();
E getLast();
E removeFirst();
E removeLast();
}