Unveiling SequencedCollection in [Java 21]: A New Era of Ordered Collections

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:

  1. Access to the first and last elements.

  2. Easy addition and removal of elements at both ends.

  3. 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

  1. 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.

  2. 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.

  3. 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

  1. 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.

  2. Enhanced Readability
    Accessing and manipulating the first and last elements no longer requires verbose or unintuitive methods (e.g., list.get(0)).

  3. Reversed Views
    The reversed() method provides an efficient way to traverse collections backward without creating a separate copy.

  4. Retrofit Support
    Existing collection classes like ArrayList, LinkedList, and TreeSet now seamlessly support the new interface.

Limitations

  1. Not Thread-Safe
    Like other non-synchronized collections, SequencedCollection implementations are not thread-safe. Use wrappers like Collections.synchronizedCollection() for thread safety.

  2. Limited to Java 21+
    This feature is only available in Java 21 and later. Older Java versions require workarounds or custom implementations.

  3. 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