Introduced in Java 21, a SequencedCollection
is a Collection
whose elements have a defined encounter order i.e. it has first and last elements, and the elements between them have successors and predecessors. Some examples include List
, Deque
, SortedSet
, and LinkedHashSet
.
The SequencedCollection
interface provides methods to add, retrieve, and remove elements at either end of the collection. It also has a reversed()
method which provides a reverse-ordered view of the original collection.
Similarly, the new SequencedMap
interface is a map that has a well-defined encounter order, supports operations at both ends, and is reversible. Examples include LinkedHashMap
and TreeMap
.
Example usage:
var set = new LinkedHashSet<String>(Arrays.asList("a", "b", "c")); set instanceof SequencedCollection ==> true set.getFirst() ==> "a" set.getLast() ==> "c" set.reversed() ==> [c, b, a]
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.