Unveiling SequencedCollection in [Java 21]: A New Era of Ordered Collections
What is SequencedCollection?
The SequencedCollection interface represents collections that maintain a defined order of elements. These collections allow for:
Access to the first and last elements.
Easy addition and removal of elements at both ends.
A reversed view of the collection, without needing to copy or reorder it manually.
Classes like ArrayList, LinkedList, TreeSet, and LinkedHashSet have been retrofitted to implement this interface, making them more versatile and consistent.
Key Features
Unified Interface for Ordered Collections
Operations on ordered collections like List, Deque, and ordered Set are standardized.
Simplifies coding by offering a consistent API for accessing and modifying collections.
New Methods in SequencedCollection
The interface introduces the following methods:addFirst(E e)
: Adds an element at the beginning.addLast(E e)
: Adds an element at the end.getFirst()
: Retrieves the first element.getLast()
: Retrieves the last element.removeFirst()
: Removes the first element.removeLast()
: Removes the last element.reversed()
: Returns a reversed view of the collection.
Retrofit to Existing Collections
Many existing collection classes have been updated to implement SequencedCollection, including:List: ArrayList, LinkedList
Set: TreeSet, LinkedHashSet
Example of Array List with SequencedCollection
import java.util.ArrayList;
import java.util.SequencedCollection;
public class ArrayListExample {
public static void main(String[] args) {
SequencedCollection<Integer> seqList = new ArrayList<>();
seqList.addFirst(1);
seqList.addFirst(2);
seqList.addLast(3);
System.out.println("Get First " + seqList.getFirst()); // 2
System.out.println("Get Last " + seqList.getLast()); // 3
System.out.println(seqList); //[2,1,3]
System.out.println(seqList.reversed()); //[3,1,2]
seqList.removeFirst();
System.out.println(seqList); // [1,3]
}
}
Example of Linked List with SequencedCollection
package SequenceCollections;
import java.util.LinkedList;
import java.util.SequencedCollection;
public class LinkedListSCExample {
public static void main(String[] args) {
SequencedCollection<Integer> seqLL = new LinkedList<>();
seqLL.addFirst(10);
seqLL.addLast(20);
System.out.println("LinkedList: " + seqLL); // [10, 20]
System.out.println("Reversed View: " + seqLL.reversed()); // [20, 10]
seqLL.removeLast();
System.out.println("After removeLast: " + seqLL); // [10]
}
}
Advantages of SequencedCollection
Consistent API
Developers can now use the same methods (addFirst, getLast, etc.) across different ordered collections, eliminating the need for type-specific casts or logic.Enhanced Readability
Accessing and manipulating the first and last elements no longer requires verbose or unintuitive methods (e.g., list.get(0)).Reversed Views
The reversed() method provides an efficient way to traverse collections backward without creating a separate copy.Retrofit Support
Existing collection classes like ArrayList, LinkedList, and TreeSet now seamlessly support the new interface.
Limitations
Not Thread-Safe
Like other non-synchronized collections, SequencedCollection implementations are not thread-safe. Use wrappers like Collections.synchronizedCollection() for thread safety.Limited to Java 21+
This feature is only available in Java 21 and later. Older Java versions require workarounds or custom implementations.Subset of Use Cases
While SequencedCollection simplifies many tasks, specialized cases (e.g., random access) may still require traditional approaches like indexed get for List.
Conclusion
The introduction of SequencedCollection in Java 21 marks a significant improvement in the way developers work with ordered collections. It simplifies APIs, improves readability, and makes common tasks like reversing and boundary element manipulation more intuitive.
With retrofitting into widely used classes like ArrayList, LinkedList, TreeSet, and LinkedHashSet, this feature is a game-changer for developers seeking consistency and simplicity. However, it’s important to remember its limitations, such as lack of thread safety and compatibility with older Java versions.
Start leveraging SequencedCollection in your Java projects today, and enjoy cleaner, more efficient code.
Github Link: Sequence Collection Link