In this article, I will provide a brief and meaningful overview of sequenced collections in Java 21.
What Is A Sequenced Collection?
According to JEP 431, a sequenced collection is a Collection
whose elements have a defined encounter order. A sequenced collection has first and last elements. Every element between the first and last one has successors and predecessors.
Encounter Order
By encounter order we mean that elements in the collection have a linear arrangement (conceptually) from the first element to the last element. Given any two elements X and Y:
- X is either before or after Y
- X is either closer to the first element or closer to the last element
- Y is either before or after X
- Y is either closer to the first element or closer to the last element
This linear arrangement is conceptual because we are not talking about their physical locations in the computer’s memory.
Reverse-Ordered View Collection
Every view collection has one backing collection that stores the actual elements. When you call the reversed method of the sequenced collection, you get a reversed ordered view collection that is also a sequenced collection. The encounter order of elements in the reverse-ordered view is the inverse of the encounter order of elements in the actual collection.
Methods in java.util.SequencedCollection<E> Interface
default void addFirst(E e)
default void addLast(E e)
default E getFirst()
default E getLast()
default E removeFirst()
default E removeLast()
SequencedCollection<E> reversed()
Sequenced Sets
A sequenced set is a collection that is both a sequenced collection and set. The java.util.SequencedSet<E> is a sub-interface of java.util.SequencedCollection<E>. Therefore, it gets all the methods of SequencedCollection. But, in case of reversed method, SequencedSet.reversed method returns SequencedSet.
SequencedSet<E> reversed()
Sequenced Map
A sequenced map is a map that has a well-defined encounter order. The encounter order of a SequencedMap
applies to key-value mappings (not individual elements). A sequenced map has first and last key-value mappings. Every mapping between the first and last one has successors and predecessors.
The java.util.SequencedMap<K,V> interface is the direct sub-interface of java.util.Map<K,V> interface.
When you call the reversed method of the sequenced map, you get a reversed ordered view of this map, that is also a sequenced map.
Consider the below program. When you run the program, you get “Ä=Apple” as first key-value mapping and “E=Eggplant” as last key-value mapping.
import java.util.SequencedMap;
import java.util.TreeMap;
public class SequencedMapDemo {
public static void main(String[] args) {
SequencedMap<String, String> treeMap = new TreeMap<>();
treeMap.put("A", "Apple");
treeMap.put("B", "Banana");
treeMap.put("C", "Cherry");
treeMap.put("D", "Date");
treeMap.put("E", "Eggplant");
System.out.println(treeMap.firstEntry());
System.out.println(treeMap.lastEntry());
}
}
When you run the below program, you get “E=Eggplant” as first key-value mapping and “A=Apple” as last key-value mapping.
import java.util.SequencedMap;
import java.util.TreeMap;
public class SequencedMapDemo {
public static void main(String[] args) {
SequencedMap<String, String> treeMap = new TreeMap<>();
treeMap.put("A", "Apple");
treeMap.put("B", "Banana");
treeMap.put("C", "Cherry");
treeMap.put("D", "Date");
treeMap.put("E", "Eggplant");
SequencedMap<String, String> reversedMap = treeMap.reversed();
System.out.println(reversedMap.firstEntry());
System.out.println(reversedMap.lastEntry());
}
}
That’s all for this article. I will try to cover more on sequenced collections, sequenced sets, and sequenced maps in future.
You can learn more from the below links:
- https://openjdk.org/jeps/431
- https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/util/SequencedCollection.html
- https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/util/SequencedSet.html
- https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/util/SequencedMap.html
If you find any significant errors or want to give me some feedback, feel free to contact me at maliksanjoykumar[@]gmail.com.
Sanjoy Kumar Malik is an experienced software architect and technologist. He is passionate about Cloud Computing, Software Architecture, and System Design. Apart from technology and software, he is an avid LinkedIn networker. You can join his 5.5+ lacs supporters on LinkedIn.