🚀 Introduction: Why SequencedCollection
in Java 21?
Before Java 21, working with ordered data structures required different methods for accessing the first and last elements.
For example:
List
→list.get(0)
,list.get(list.size() - 1)
Deque
→deque.getFirst()
,deque.getLast()
Set
(e.g.,LinkedHashSet
) → No direct way to get the first/last element
💡 Java 21 introduces SequencedCollection
, a new interface that provides consistent methods for handling ordered data structures.
📌 In this article, you’ll learn:
✅ What SequencedCollection
is and why it matters
✅ How it improves List
, Set
, and Deque
✅ Complete examples demonstrating each SequencedCollection
type
🔍 What is SequencedCollection
in Java 21?
SequencedCollection
is a new interface in Java 21 that provides a unified way to manage ordered data in collections.
📌 Key Features of SequencedCollection
✅ Access first & last elements easily (getFirst()
, getLast()
)
✅ Reverse collections (reversed()
)
✅ Efficient insertion/removal at both ends (addFirst()
, addLast()
, removeFirst()
, removeLast()
)
Classes Implementing SequencedCollection
:
1️⃣ SequencedList
(For ordered List
implementations)
2️⃣ SequencedSet
(For ordered Set
implementations)
3️⃣ SequencedDeque
(For Deque
structures like ArrayDeque
and LinkedList
)
🚀 1️⃣ Using SequencedList
(Ordered Lists with First & Last Access)
Before Java 21, retrieving the first and last elements in a List
was verbose.
✔ Traditional Way (Before Java 21)
List<String> names = new ArrayList<>(List.of("Raj", "Amit", "Neha"));
// Getting first and last elements
String first = names.get(0);
String last = names.get(names.size() - 1);
System.out.println("First: " + first + ", Last: " + last);
📌 Problems:
❌ Hardcoded indices (0
and size() - 1
)
❌ Prone to IndexOutOfBoundsException
✅ Java 21 SequencedList
(Better Way)
import java.util.*;
public class SequencedListExample {
public static void main(String[] args) {
SequencedList<String> names = new ArrayList<>(List.of("Raj", "Amit", "Neha"));
System.out.println("First: " + names.getFirst()); // ✅ Direct access
System.out.println("Last: " + names.getLast());
names.addFirst("Vikram"); // ✅ Adds at the beginning
names.addLast("Priya"); // ✅ Adds at the end
System.out.println("Updated List: " + names);
System.out.println("Reversed List: " + names.reversed()); // ✅ Reverse list
}
}
📌 Advantages:
✅ Cleaner syntax (getFirst()
, getLast()
)
✅ Easy insertion/removal at both ends (addFirst()
, addLast()
)
✅ Reversing a list is now a single call (reversed()
)
🚀 2️⃣ Using SequencedSet
(Ordered Sets with First & Last Elements)
Before Java 21, Set
collections did not allow direct access to the first and last elements.
✔ Traditional Way (Before Java 21)
Set<String> cities = new LinkedHashSet<>(Set.of("Mumbai", "Delhi", "Bangalore"));
String first = cities.iterator().next(); // Gets first element (Inefficient)
System.out.println("First city: " + first);
📌 Problems:
❌ No built-in way to get the last element
❌ Must iterate manually
✅ Java 21 SequencedSet
(Better Way)
import java.util.*;
public class SequencedSetExample {
public static void main(String[] args) {
SequencedSet<String> cities = new LinkedHashSet<>(Set.of("Mumbai", "Delhi", "Bangalore"));
System.out.println("First City: " + cities.getFirst()); // ✅ Direct access
System.out.println("Last City: " + cities.getLast());
cities.addFirst("Kolkata"); // ✅ Adds at the beginning
cities.addLast("Chennai"); // ✅ Adds at the end
System.out.println("Updated Set: " + cities);
System.out.println("Reversed Set: " + cities.reversed()); // ✅ Reverse order
}
}
📌 Advantages:
✅ First and last element access without iteration
✅ Maintains order while allowing first/last insertions
✅ Reversing a set is now simple
🚀 3️⃣ Using SequencedDeque
(Efficient Double-Ended Queues)
Before Java 21, Deque
(double-ended queue) had separate methods:
addFirst()
,addLast()
getFirst()
,getLast()
💡 Now, SequencedDeque
unifies the API with SequencedCollection
methods.
✅ Java 21 SequencedDeque
Example
import java.util.*;
public class SequencedDequeExample {
public static void main(String[] args) {
SequencedDeque<Integer> numbers = new ArrayDeque<>(List.of(10, 20, 30));
System.out.println("First: " + numbers.getFirst()); // ✅ Unified API
System.out.println("Last: " + numbers.getLast());
numbers.addFirst(5); // ✅ Adds at the beginning
numbers.addLast(40); // ✅ Adds at the end
numbers.removeFirst(); // ✅ Removes first element
numbers.removeLast(); // ✅ Removes last element
System.out.println("Updated Deque: " + numbers);
System.out.println("Reversed Deque: " + numbers.reversed()); // ✅ Reverse order
}
}
📌 Advantages:
✅ Unified API across collections
✅ Efficient insertions and removals from both ends
✅ Easy to reverse order
🔥 Key Takeaways
✅ Java 21’s SequencedCollection
provides a cleaner API for ordered data structures.
✅ SequencedList
simplifies list operations (getFirst()
, getLast()
, reversed()
).
✅ SequencedSet
allows direct access to the first & last elements without iteration.
✅ SequencedDeque
unifies the API for efficient queue operations.
By adopting SequencedCollection
, your Java code will be more readable, efficient, and easy to maintain! 🚀
Comments
Post a Comment
Leave Comment