👋 Introduction
Have you ever used a list or a set and needed to keep track of the order of items manually?
Maybe you wanted to get the first or last item, but had to write extra code?
Good news! We now have Sequenced Collections in Java 21 to solve this problem.
It makes your code easier, cleaner, and faster to understand.
Let’s learn what it is — in simple English!

🔍 What Are Sequenced Collections in Java 21?
Sequenced Collections are a new type of collection that keep the insertion order and let you:
✅ Access the first and last elements easily
✅ Add elements to the front or back
✅ Loop through items in order
📦 Which Collections Are Sequenced?
In Java 21, these collections now support the new SequencedCollection
interface:
List
Deque
LinkedHashSet
NavigableSet
They now have new methods that make working with them easier.
🧪 Simple Example with List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.*; public class Main { public static void main(String[] args) { List<string> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); System.out.println("First: " + names.getFirst()); System.out.println("Last: " + names.getLast()); } } |
📝 Output:
1 2 3 4 5 |
First: Alice Last: Charlie |
Before Java 21, you had to use get(0)
and get(size - 1)
. Now it’s much clearer!
🔁 Reverse Order Easily
You can even reverse the collection using reversed()
:
1 2 3 4 5 |
List<String> names = List.of("A", "B", "C"); List<String> reversed = names.reversed(); System.out.println(reversed); // [C, B, A] |
➕ Add to Front or Back
For Deques and other double-ended collections:
1 2 3 4 |
Deque<string> items = new ArrayDeque<(); items.addFirst("Start"); items.addLast("End");</string> |
This makes it perfect for working with queues and stacks.
💡 Why Is This Useful?
Before Java 21 | After Java 21 |
---|---|
Manual indexing | getFirst() , getLast() |
Loop and reverse manually | Use reversed() |
Confusing code | Clean and readable |
✅ Sequenced Interfaces Summary
Interface | Description | Implemented By |
---|---|---|
SequencedCollection<E> | Ordered collection, new API | LinkedList , Deque , ArrayDeque |
SequencedSet<E> | Ordered set with sequence operations | LinkedHashSet |
SequencedMap<K, V> | Ordered map with entry control | LinkedHashMap |
✅ SequencedCollection<E>
Extends: Collection<E>
📌 Purpose:
Provides a standard way to access first, last, and reverse order of elements in any ordered collection.
🧩 Key Methods:
1 2 3 4 5 |
E getFirst(); // Retrieves the first element E getLast(); // Retrieves the last element SequencedCollection<E> reversed(); // Returns a reversed view |
🚀 Implemented By:
Deque
(e.g.,ArrayDeque
,LinkedList
)LinkedHashSet
- Any collection with a predictable order
✅ SequencedSet<E>
Extends: Set<E>
, SequencedCollection<E>
📌 Purpose:
Extends the Set interface for ordered sets with sequence-related operations.
🧩 Key Features:
- Inherits all methods from
SequencedCollection
- Maintains insertion order
- Still ensures no duplicate elements
🚀 Implemented By:
LinkedHashSet<E>
✅ SequencedMap<K, V>
Extends: Map<K, V>
📌 Purpose:
Provides sequence-based operations on maps that preserve entry order.
🧩 Key Methods:
1 2 3 4 5 |
Map.Entry<K, V> firstEntry(); // Retrieves first key-value pair Map.Entry<K, V> lastEntry(); // Retrieves last key-value pair SequencedMap<K, V> reversed(); // Returns reversed view of the map |
🚀 Implemented By:
LinkedHashMap<K, V>
📌 Summary
Feature | Benefit |
---|---|
getFirst() | Easy access to first item |
getLast() | Easy access to last item |
addFirst() /addLast() | Add to front or back |
reversed() | Get reversed view of collection |
💬 Final Thoughts
Sequenced Collections in Java 21 are a small change — but they make a big difference in how we write code.
No more get(0)
or get(size - 1)
— now your intentions are clear. It’s easier for both you and others to read your code.
If you’re learning Java, this is a great new feature to add to your toolkit!
Reference : Sequenced Collections