Return to site

☕2️⃣1️⃣ Java 21's New (Sequenced) Collections - Inside Java Newscast

· java

Java 21 is getting "sequenced collections" - making it easier to do stuff such as getting the last element of a collection, or reversing its elements. See more details in this video:

1️⃣ SequencedCollection

:

void addFirst(E);

void addLast(E);

E getFirst();

E getLast();

E removeFirst();

E removeLast();

SequencedCollection reversed();

2️⃣ SequencedSet

:

void addFirst(E);

void addLast(E);

E getFirst();

E getLast();

E removeFirst();

E removeLast();

SequencedSet reversed();

3️⃣ SequencedMap

V putFirst(K,V)

V putLast(K,V)

Entry firstENtry()

Entry lastEmtry()

Entry pollFirstEntry()

Entry pollLastEntry()

SequencedMap

👍 Benefits:

No more list.get(list.size()-1); to get the last list element.

No more tedious iteration in reverse order:

from

list.listIterator(...).previous() use

to

for(E el: list.reversed()){...}