DEV Community

msmittalas
msmittalas

Posted on

"Sequenced Collections": an upcoming feature of Java 21 to handle a sequence of elements in a better way.

Java's Collection API is one of the oldest and active API in JDK.
It has collection type like List, Set and Map. However, A collection type that represents a sequence of elements with a specified encounter order and standard set of operation is absent from Java's collections framework.

For instance, List and Deque both specify an encounter order, but "Collection", their shared supertype, does not. Similar to how Set does not define an encounter order, neither do subtypes like HashSet or SortedSet, although LinkedHashSet and SortedSet do. As a result, encounter order support is dispersed throughout the type of hierarchy, making it challenging to express some beneficial ideas in APIs. A parameter or return result with an encounter order cannot be described by either a collection or a list.

To solve this problem, new interfaces for sequenced collections, sequenced sets, and sequenced maps have been developed, which have then been retrofitted into the type hierarchy of the current collection. There are default implementations for each new method that is declared in these interfaces. Sequenced collections represent a collection whose elements have a defined encounter order, sequenced sets represent a set that is a sequenced collection but does not contain duplicate elements, and sequenced maps represent a map whose entries have a defined encounter order. Each of these objects has a unique set of properties.
All the various sequenced types can process elements in both directions using all the common iteration mechanisms, including enhanced for loops, explicit iterator() loops, forEach(), stream(), parallelStream(), and toArray(), thanks to the new reversed()method, which provides a reverse-ordered view of the original collection.

Overall, the Java Collections Framework has advanced significantly with the addition of new interfaces to represent collections with a specified encounter order and a uniform set of actions that are applicable across such collections. For developers, the framework will become more understandable and effective by offering support for encounter order in a standardised and simple-to-use manner.

more details on: https://openjdk.org/jeps/431

Top comments (0)