118. Tackling Sequenced Collections
The Sequenced Collections API was added as a final feature in JDK 21 under JEP 431. Its main goal is to make the navigation of Java collections easier by providing a common API to all collections having a well-defined encounter order.
A Java collection with a well-defined encounter order has a well-defined first element, second element, and so on, until the last element. The encounter order is the order in which an Iterator
will iterate the elements of a collection (list, set, sorted set, map, and so on). The encounter order can take advantage of stability over time (lists) or not (sets).
This API consists of 3 interfaces named SequencedCollection
(valid for any collection that has a well-defined encounter order), SequencedSet
(extends SequencedCollection
and Set
to provide support for Java sets), and SequencedMap
(extends Map
to give support to any Java map that has a well-defined encounter order). In the following diagram, you can see...