Sitemap
Nick Lim

Nick Lim

Software Engineer; Java; Spring Boot; jPOS https://github.com/weeshin

Member-only story

Java 21: SequencedCollection

2 min readDec 13, 2023

--

Introduction

With the release of Java 21, we have gained access to a new interface: SequencedCollection. A new feature is introduced in Collection classes, allows us to access the first and last elements of the collection data. There is a cool feature that allow us to reverse the sequence of data. We will explore the feature of the SequencedCollection.

What is SequencedCollection?

The SequencedCollection interface provides methods to add, retrieve, and remove elements at both ends of the collection.

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

In the code snippet below, we conduct tests the `addFirst()` method to insert an element into SequencedCollection. Subsequently, we utilize `getFirst()` and `getLast()` to retrieve the first and last elements. Additionally, we explore the functionality of `reversed()` method to reverse the data ordering. These tests serve as a demonstration of the new features introduced in the SequencedCollection.

@Test
public void sequenceCollectionTest() {
// Creating a SequencedCollection of languages
SequencedCollection<String> languages =…

--

--