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

Image of Datadog

Optimize UX with Real User Monitoring

Learn how Real User Monitoring (RUM) and Synthetic Testing provide full visibility into web and mobile performance. See best practices in action and discover why Datadog was named a Leader in the 2024 Gartner MQ for Digital Experience Monitoring.

Tap into UX Best Practices

Top comments (0)

Image of Datadog

Keep your GPUs in check

This cheatsheet shows how to use Datadog’s NVIDIA DCGM and Triton integrations to track GPU health, resource usage, and model performance—helping you optimize AI workloads and avoid hardware bottlenecks.

Get the Cheatsheet

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay