Sequenced Collections: A Dive into Java — Java 21

Bayram EKER
2 min readOct 23, 2023

--

Introduction

In computer programming, the order of collections is an intrinsic facet of algorithm and data structure design. Java, as a widely-used programming language, has evolved its collection framework, but gaps remain. This article delves into the need for collections with a defined encounter order in Java and introduces new interfaces to bridge this gap.

“Life can only be understood backwards; but it must be lived forwards.”
— Kierkegaard

The Motivation

Java’s collections framework is robust, but it lacks a definitive type representing a sequence of elements with a defined encounter order. Such an absence has led to inconsistencies and operational limitations, prompting numerous developer complaints.

Consider the scenario:

List<String> myList = new ArrayList<>();
Deque<String> myDeque = new ArrayDeque<>();

Both List and Deque define an encounter order. But their common ancestor, Collection, does not. This incongruence poses challenges when designing APIs that can uniformly handle both.

The Problem at Hand

The discrepancies in the framework are evident when we examine operations related to the encounter order:

  • To get the first element of a List, you'd use list.get(0). But for the last element? list.get(list.size() - 1). This feels unnecessarily cumbersome.
  • For Deque, there's deque.getFirst() and deque.getLast(), which are more intuitive.
  • And then there’s LinkedHashSet, which doesn't even have a straightforward method for fetching the last element!

Proposed Solution

The article suggests introducing new interfaces for sequenced collections, sets, and maps in Java. These interfaces, integrated into the existing hierarchy, would streamline operations and provide a consistent approach for developers.

Sequenced Collections

A proposed SequencedCollection would extend the Collection interface and introduce methods like:

interface SequencedCollection<E> extends Collection<E> {
SequencedCollection<E> reversed();
void addFirst(E);
void addLast(E);
E getFirst();
E getLast();
E removeFirst();
E removeLast();
}

With this, obtaining a reverse-ordered stream from a LinkedHashSet becomes straightforward:

linkedHashSet.reversed().stream()

Retrofitting the Java Collections Framework

To ensure backward compatibility and a seamless developer experience, these new interfaces must be carefully integrated into the existing Java collections framework.

For example:

List<String> myList = new ArrayList<>();
SequencedCollection<String> mySequencedCollection = (SequencedCollection<String>) myList;

This would allow developers to leverage the power of both the traditional and new interfaces.

Conclusion

Java’s collection framework is powerful, but there’s always room for improvement. By introducing sequenced collections, sets, and maps, Java can offer developers a more intuitive and streamlined approach to handling ordered data structures.

Happy Coding! 🚀

Thank you for taking the time to read my thoughts and musings here on Medium! If you found any value or resonance in what you’ve read, please consider giving this article some claps👏. It means a lot to me and encourages me to keep sharing with this wonderful community. Stay tuned for more, and don’t forget to spread love and kindness wherever you go. Until next time! ✨

--

--

No responses yet