Java 21 – Sequenced Collections

How do you get the last element of a list in Java?

Using a code similar to below:

String lastElement = listOfElements.get(listOfElements.size()-1)

That doesn’t look elegant!

And how do you get the last element for a Sorted Set?

String lastElement = sortedSet.last();


That looks much more elegant but then different collections are having different ways to get the last element.

There is no standard way of doing it.

If you take a LinkedHashSet , the only way to get the last element is to iterate through the entire collection till the last element.

Similarly there is no standard way to iterate a collection in the reverse.

To solve these issues , Java 21 provides Sequenced Collection.

Existing collections are converted to Sequenced Collections.

These provide some standard methods to :

  • Get the first element of the collection
  • Get the last element
  • Add an element to the beginning of the collection
  • Add an element to the end
  • Reverse a collection
  • Remove first element
  • Remove last element

Here is the interface :

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

All the collections extend/implement this interface directly or indirectly so you can make use of these methods in any collection.

So now to get the last element of a list , all you need to do is:

String lastElement = listOfElements.getLast();

A simpler and standard way of getting the last element!

Similarly to reverse a list , you just need to do :

listOfElements.reverse();

This will return a stream of elements and then you can use the usual way of iterating through a stream using .forEach() method.

This holds true for all the collections like the different types of Set and Map.

You can reverse any collection using the method reverse()!

Here is the reversed collection hierarchy:

The ones highlighted in Green are the new interfaces introduced by Java .

So List directly extends SequencedCollection

Set implementations extend SequencedSet

Map implementations extend SequencedMap

SequencedSet also extends SequencedCollection interface with just one overrided method for reverse() method.

Instead of returning a stream of Sequenced Collection , the overrided reverse method returns a stream of Sequenced Set:

interface SequencedSet<E> extends Set<E>, SequencedCollection<E> {
    SequencedSet<E> reversed();
}

SequencedMap interface provides the following functions:

interface SequencedMap<K,V> extends Map<K,V> {
    // new methods
    SequencedMap<K,V> reversed();
    SequencedSet<K> sequencedKeySet();
    SequencedCollection<V> sequencedValues();
    SequencedSet<Entry<K,V>> sequencedEntrySet();
    V putFirst(K, V);
    V putLast(K, V);

    Entry<K, V> firstEntry();
    Entry<K, V> lastEntry();
    Entry<K, V> pollFirstEntry();
    Entry<K, V> pollLastEntry();
}

So we have got new standard methods to our Java collections.

But what if any of the collection cannot support any of these methods.

For example,

A Sorted Set cannot allow you to add an element to the first or to the last.

so you cannot call sortedSet.addFirst() method.

Because then it cant be a sorted set.

So in this case you will get UnsupportedOperationException.

Thats it!

Reference:

https://openjdk.org/jeps/431


Posted

in

, ,

by

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading