💡 Did you know that Sequenced Collections were introduced in Java 21 to solve the lack of a unified API for ordered collections? Before Java 21, many collections were already "sequenced" (such as: List, LinkedHashSet, LinkedHashMap, TreeSet, TreeMap) However, they didn't share a common interface that reflected this property. This led to inconsistent APIs. For example: • To get the first element of a List, you'd use list.get(0), but there was no equivalent for an ordered Set or Map. • To get the last element of a List, you'd use list.get(list.size() - 1), while for a LinkedHashSet, you'd have to iterate through the entire set. • To reverse a list you could use Collections.reverse(list), but for a LinkedHashSet or TreeMap, you’d need to copy elements into another structure. 🔧 Sequenced Collections fix this by introducing new interfaces: 🔹 SequencedCollection<E>: The main interface, which extends Collection<E>. It represents any collection with a defined order. 🔹SequencedSet<E>: Extends SequencedCollection<E> and Set<E>. Implemented by classes like LinkedHashSet. 🔹SequencedMap<K, V>: Extends Map<K, V>. Implemented by classes like LinkedHashMap. Existing classes like List, Deque, LinkedHashSet, and LinkedHashMap have been retrofitted to implement these new interfaces. 🔑 Key Methods The interface SequencedCollection provides a standard set of methods for accessing elements at either end: • getFirst(): Gets the first element • getLast(): Gets the last element • addFirst(E): Adds an element to the beginning. • addLast(E): Adds an element to the end. • removeFirst(): Removes and returns the first element. • removeLast(): Removes and returns the last element. • reversed(): This is a key feature. It returns a reverse-ordered view of the collection. #Java #Java21 #SequencedCollections #SoftwareDevelopment #LearningJava
That’s a great update, and it relates to something I wrote about some time ago: the absence of a get() method in Set. Back then, I realized that Set intentionally doesn’t expose positional access, because order wasn’t guaranteed. But with Sequenced Collections, Java now bridges that gap by introducing a unified API for ordered access, without breaking the existing Set contract. This is a perfect example of how elegant Java evolution is. Instead of adding new methods to old interfaces, Java formalized the concept of “sequence” itself.
Excellent overview of one of Java 21’s most elegant improvements. Thanks for clearly showing how Sequenced Collections bring long-awaited consistency to ordered data structures.
I'm excited to see the introduction of Sequenced Collections in Java 21, which provides a unified API for ordered collections and simplifies operations like accessing first/last elements and reversing sequences. This will undoubtedly improve developer efficiency and code readability.
Marcos, what a great reminder about Java 21's Sequenced Collections! It's one of those refinements that, while seemingly small, brings an API consistency that greatly improves developer life and code readability. The reversed() method is a game-changer for operations on LinkedHashSet and TreeMap. Do you think this standardization will reduce the need to use Deque in scenarios where only start/end access is required? Excellent post!