Sequenced Collections in Java 21: Say Goodbye to Manual Ordering

👋 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!

Sequenced Collections in Java 21: Say Goodbye to Manual Ordering

🔍 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

📝 Output:

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():

➕ Add to Front or Back

For Deques and other double-ended collections:

This makes it perfect for working with queues and stacks.

💡 Why Is This Useful?

Before Java 21After Java 21
Manual indexinggetFirst(), getLast()
Loop and reverse manuallyUse reversed()
Confusing codeClean and readable

✅ Sequenced Interfaces Summary

InterfaceDescriptionImplemented By
SequencedCollection<E>Ordered collection, new APILinkedList, Deque, ArrayDeque
SequencedSet<E>Ordered set with sequence operationsLinkedHashSet
SequencedMap<K, V>Ordered map with entry controlLinkedHashMap

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:

🚀 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:

🚀 Implemented By:

  • LinkedHashMap<K, V>

📌 Summary

FeatureBenefit
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