1. Introduction
Java 21 introduced a powerful enhancement to the Collections Framework called Sequenced Collections. This feature provides a standardized way to work with collections that have a well-defined encounter order, making it easier to access, traverse, and manipulate elements from both ends. It unifies common behaviors across lists, deques, and ordered sets by introducing new interfaces that simplify previously inconsistent APIs.
Sequenced Collections are especially useful in modern enterprise applications that require predictable ordering, such as event processing, task scheduling, and real-time data pipelines.
2. Problem
Before Java 21, working with ordered collections in Java had several challenges:
- Different APIs for similar behaviors
- No common abstraction for ordered collections
- Complex reverse traversal
- Error-prone head/tail operations
These inconsistencies increased maintenance costs and caused subtle bugs in production systems.
3. Solution
Java 21 added three new interfaces to solve these problems:
- SequencedCollection<E>
- SequencedSet<E>
- SequencedMap<K, V>
These interfaces provide a unified contract to access and manipulate ordered data more consistently.
- getFirst()
- getLast()
- addFirst(E e)
- addLast(E e)
- removeFirst()
- removeLast()
- reversed()
4. Implementation
Real-Life Scenario: Processing Real-Time Purchase Orders in an E-Commerce System
Imagine an e-commerce back-end system that processes purchase orders as they arrive. The system must:
- Process the oldest order first (FIFO)
- Handle urgent priority orders by placing them at the front
- Inspect the newest and oldest orders without removing them
- Reverse the processing order for auditing or replay
Order Model
public class Order {
private final String id;
private final double amount;
public Order(String id, double amount) {
this.id = id;
this.amount = amount;
}
public String getId() {
return id;
}
public double getAmount() {
return amount;
}
@Override
public String toString() {
return "Order{id='" + id + "', amount=" + amount + "}";
}
}Using SequencedCollection
import java.util.LinkedList;
import java.util.SequencedCollection;
public class OrderProcessor {
private final SequencedCollection<Order> orderQueue = new LinkedList<>();
public void addNormalOrder(Order order) {
orderQueue.addLast(order);
}
public void addPriorityOrder(Order order) {
orderQueue.addFirst(order);
}
public Order peekOldestOrder() {
return orderQueue.getFirst();
}
public Order peekNewestOrder() {
return orderQueue.getLast();
}
public Order processNextOrder() {
return orderQueue.removeFirst();
}
public void replayOrdersInReverse() {
SequencedCollection<Order> reversed = orderQueue.reversed();
reversed.forEach(System.out::println);
}
}Running the Example
public class MainApp {
public static void main(String[] args) {
OrderProcessor processor = new OrderProcessor();
processor.addNormalOrder(new Order("ORD-1001", 250.00));
processor.addNormalOrder(new Order("ORD-1002", 175.50));
processor.addPriorityOrder(new Order("ORD-PRIORITY-1", 999.99));
System.out.println("Oldest Order: " + processor.peekOldestOrder());
System.out.println("Newest Order: " + processor.peekNewestOrder());
System.out.println("Processing: " + processor.processNextOrder());
System.out.println("Replaying orders in reverse:");
processor.replayOrdersInReverse();
}
}5. Conclusion
Sequenced Collections in Java 21 provide a unified and modern way to work with ordered data structures. By standardizing access to the first and last elements and offering native reverse traversal, they significantly reduce boilerplate and improve code readability.
For real-world systems such as e-commerce, event streaming, and task scheduling, Sequenced Collections help engineers build more robust and predictable Java applications.