JEP 431: Sequenced Collections has been promoted from Candidate to Proposed to Goal standing for JDK 21. It proposes introducing “a brand new household of interfaces that symbolize the idea of a set whose parts are organized in a well-defined sequence or ordering, as a structural property of the gathering.” That is motivated by the necessity for a well-defined ordering and a uniform set of operations throughout the Collections Framework.
Java’s collections framework has lengthy wanted a set sort that represents a sequence of parts with an outlined encounter order and a uniform set of operations that apply throughout such collections. Sadly, help for encounter order is at present unfold throughout the sort hierarchy, making expressing sure sensible ideas in APIs troublesome. In response, a proposal has been proposed to introduce new interfaces to symbolize collections with an outlined encounter order and supply uniform APIs for accessing their first and final parts and processing them in reverse order.
The shortage of a set sort representing a sequence of parts with an outlined encounter order has been a repeated supply of issues and complaints. For instance, whereas Checklist
and Deque
outline an encounter order, their frequent supertype is Assortment
, which doesn’t. Equally, Set
doesn’t specify an encounter order, and subtypes comparable to HashSet
don’t outline one, however subtypes comparable to SortedSet
and LinkedHashSet
do.
To handle this situation, new interfaces have been outlined for sequenced collections, sequenced units, and sequenced maps after which retrofitted into the present assortment’s sort hierarchy. The entire new strategies declared in these interfaces have default implementations. Sequenced collections, units, and maps all have completely different traits, with sequenced collections representing a set whose parts have an outlined encounter order, sequenced units representing a set that could be a sequenced assortment that comprises no duplicate parts, and sequenced maps representing a map whose entries have an outlined encounter order.
The brand new reversed()
methodology gives a reverse-ordered view of the unique assortment, enabling all of the completely different sequenced varieties to course of parts in each instructions utilizing all the same old iteration mechanisms, comparable to enhanced for loops, express iterator()
loops, forEach()
, stream()
, parallelStream()
, and toArray()
.
The SequencedCollection
interface contains a number of new strategies, as follows::
interface SequencedCollection<E> extends Assortment<E> {
// new methodology
SequencedCollection<E> reversed();
// strategies promoted from Deque
void addFirst(E);
void addLast(E);
E getFirst();
E getLast();
E removeFirst();
E removeLast();
}
The SequencedSet
interface contains the identical strategies as SequencedCollection
, plus reversed()
. Lastly, the SequencedMap
interface contains a number of new strategies, as follows:
interface SequencedMap<Ok,V> extends Map<Ok,V> {
// new strategies
SequencedMap<Ok,V> reversed();
SequencedSet<Ok> sequencedKeySet();
SequencedCollection<V> sequencedValues();
SequencedSet<Entry<Ok,V>> sequencedEntrySet();
V putFirst(Ok, V);
V putLast(Ok, V);
// strategies promoted from NavigableMap
Entry<Ok, V> firstEntry();
Entry<Ok, V> lastEntry();
Entry<Ok, V> pollFirstEntry();
Entry<Ok, V> pollLastEntry();
}
All three new interfaces match neatly into the present collections sort hierarchy, with Checklist
having SequencedCollection
as its quick superinterface, Deque
having SequencedCollection
as its quick superinterface, LinkedHashSet
implementing SequencedSet
, SortedSet
having SequencedSet
as its quick superinterface, LinkedHashMap
implementing SequencedMap
, and SortedMap
having SequencedMap
as its quick superinterface.
Whereas explicit-positioning APIs comparable to SortedSet::addFirst
and SortedMap::putLast
throw UnsupportedOperationException
as a result of the sequence of their parts is set by relative comparability, the asymmetry of getting some collections not implement all the SequencedCollection
operations is efficacious as a result of it brings SortedSet
and SortedMap
into the sequenced assortment household, permitting them for use extra broadly than in any other case.
Total, introducing new interfaces to symbolize collections with an outlined encounter order and a uniform set of operations that apply throughout such collections is a major step ahead for Java’s Collections Framework. By offering help for encounter order in a constant and easy-to-use method, the framework will develop into extra intuitive and environment friendly for builders.