Welcome to the Java JDK 21 Series where we cover all the new features that are introduced in this latest LTS Version.
Introduction to JEP 431
The JEP 431 is known as Sequenced Collections, It introduces some new interfaces and methods to represent collections in Java that maintain a specific order for their elements. This JEP addresses the lack of a standardized way to work with ordered collections in Java's existing collections framework, which has led to inconsistencies and limitations in working with such collections.
The main motivations are to resolve the below questions related to ordered collections in Java,
Variability in ordered elements support.
Lack of consistency in supertype.
Absence of standardized methods.
Complexity in Reverse Iteration.
What's are Sequenced Collections
These are three new interfaces that are added to the Java Collections framework SequencedCollection, SequencedSet and SequencedMap.
As you may see prior to Java JDK 21, we can fetch the first and the last element of an ArrayList as follows,
Do take a look at the other methods introduced in SequencedCollection.java which are self explanatory.
default E getFirst()
default E getLast()
default E removeFirst()
default E removeLast()
SequencedCollection<E> reversed()
Similarly lets take a look at SequencedSet. The Sequenced Set is a Set that is also a Sequenced Collection with no duplicate elements, It inherits the methods from the Sequenced Collection and includes specialized methods for adding elements while maintaining order.
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!