Java 21: SequencedCollection, SequencedSet and
SequencedMap
Tuesday, 20 May 2025 5:38 PM
SequencedCollection, SequencedSet and
SequencedMap
• These are new interfaces added in existing Java Collection hierarchy.
So, before we start understanding about these new interfaces,
Let's first see, existing Collection hierarchy and where does these new interfaces
fit into that:
Existing Hierarchy:
We have already covered each type of collection in depth in previous videos, kindly check it
out, if there is any doubt with above hierarchy diagram and topics:
New Collection Hierarchy (Java
21+):
New interfaces
Existing Interfaces
Existing Concrete
Class
Iterable
Collection
extends
implements
Queue SequencedCollection Set
extends
extends extends
extends SequencedSet
List HashSet
PriorityQueue Deque implements
extends
SortedSet
LinkedHashS
ArrayList Vector et
NavigableSet
ArrayDeque LinkedList Stack
TreeSet
Map
implements
extends
SequencedMap HashMap HashTable
implements
extends
SortedMap LinkedHashMap
NavigableMap
TreeMap
Three questions comes to our mind is:
1. What's the criteria of putting these interfaces at this specific place? Like I can see it left out
Queue and PriorityQueue, why? Why not include those also?
2. Why exactly these interfaces required? What is the gap, which it is trying to fill.
3. Why SequencedSet is needed, why not SequencedCollection is sufficient for those
collections group?
Once, we understand above points, we will fully understand these SequencedCollection, SequencedSet and
SequencedMap
1.
What's the criteria of putting these interfaces at this specific place? Like I can see it left out Queue
and PriorityQueue, why? Why not include those also?
Any Collection which follows below conditions, can be termed as Sequenced
• Collection should follow Predictable Iteration :
Means elements are returned in consistent and well defined order every time we iterate over the
collection.
So if a collection maintains elements in:
• Insertion order, or
• Sorted order (e.g., ascending or descending)
Then we can say, it follows Predictable iteration.
• Collection provide support for Access or Manipulate the First and Last element
• Collection supports Reversible View
Collection Predictable First & Last Reversible View Part of
Iteration Element operation
Follows Insertion Access: List<String> list = new ArrayList<> SequencedCollection
List Order [Link](0); ([Link]("a","b","c"));
[Link]([Link]() - 1); [Link](list);
Add:
[Link](0, element);
[Link](element);
Remove:
[Link](0);
[Link]([Link]() - 1);
Follows Insertion Access:
Deque Order SequencedCollection
[Link]();
Deque<Integer> revDeque = [Link]();
[Link]();
Add:
[Link](element);
[Link](element);
Remove:
[Link]();
[Link]();
Queue Follows Insertion Follows FIFO
Order (insertion at back and remove from Not supported directly
front)
N/A
Access:
[Link](); //return head
Access Last element -
Add:
Add element at first -
[Link](element); //add to tail
Remove:
[Link](); //removes head
Remove last element-
PriorityQueue Do no follow either
Access: Not supported
Insertion or Sorted
order
[Link](); //return top priority val N/A
Access Last element -
Why? bcoz Add:
Add element at first -
• Uses heap.
Add element at last-
• Which guarantees As addition at first or last happens
the head is either based on priority
minimum or
maximum element, Remove:
depending on the [Link](); //removes top priority val
comparator. Remove last element-
• But the iteration
order is not
properly sorted or
consistent.
Not supported
HashSet Do no follow either Since order is not supported, access N/A
Insertion or Sorted first or last element is not supported
order too.
LinkedHashSe Follows Insertion Access:
Order Requires manual iteration and then we can
t access both first and last element. As LinkedHashSet, uses doubly linked list
ex: internally, so its possible to get reverse view of the
[Link]().next(); //first element collection. SequencedSet,
Access First Element: But there is no API or method exposed before
Reason why
Access Last Element: Java21.
LinkedHashSet, is
put inside
Add: SequencedSet and
not directly under
Add element at First: SequencedCollection
(Yellow because, it uses doubly because,
LinkedList internally, so its possible to SequencedSet adds
add element at First, but method is not an additional contract
present)
i.e. NO
[Link]("A"); — Adds to the end DUPLICATES.
Remove:
Requires manual iteration and then we can
remove both first and last element.
ex:
Iterator<E> it = [Link]();
[Link]();
[Link]();
Remove First Element:
Remove Last Element:
SortedSet<Integer> sortedSet = new
TreeSet<>([Link](14, 5, 7)); NavigableSet<Integer> sortedSet = new
SortedSet Follows Sorted TreeSet<>([Link](14, 5, 7));
Order Access:
sortedSet .first(); // return 5 (minimum)
sortedSet .last(); // return 14 Iterator<String> revItr = SequencedSet
(maximum) [Link]();
while([Link]()) {
Add: //iteration logic
sortedSet .add(2); // Goes to first if }
smallest
sortedSet .add(25); // Goes to last if
largest
Remove:
sortedSet .remove(sortedSet .first());
sortedSet .remove(sortedSet .last());
Since order is not supported, access Not supported
Do no follow first or last element is not supported
HashMap too.
N/A
Since order is not supported, access Not supported
Do no follow first or last element is not supported
HashTable too.
N/A
Follows Insertion
LinkedHashM Order
SequencedMap
ap Access:
[Link]().iterator().next() (Reverse view is possible as it uses doubly
Similarly last element can be LinkedList, but method is not present)
iterate.
Add:
Add element at First:
(Yellow because, it uses doubly LinkedList
internally, so its possible to add element at
First, but method is not present)
[Link](key, value)
It adds the entry at the end of the map
iteration order.
Remove:
Requires manual iteration and then we can
remove both first and last element.
ex:
[Link]().iterator().remove()
Remove First Element:
Remove Last Element:
Access: In NavigableMap, we have the method
SortedMap Follows Sorted "descendingMap()", we can use it to iterate in
firstKey()
Order lastKey() reverse direction. SequencedMap
Add:
put(K key, V value)
Remove:
pollFirstEntry() (via
NavigableMap)
pollLastEntry() (via
NavigableMap)
2. Why exactly these interfaces required? What is the gap, which it is
trying to fill.
• If we observe the Collections which are Sequenced. We will find that:
• Get first
• Get Last
• Add first
• Add last
• Remove first
• Remove last
• Reverse View of the collection
There is no common interface, each collection have their own method. Which is difficult to remember and
maintain.
That’s one of the gap, which these SequencedCollection, SequencedSet and SequencedMap are trying to fill
up.
SequencedCollection SequencedMap
• Void addFirst(E e) • [Link]<K,V> firstEntry()
• Void addLast(E e) • [Link]<K,V> lastEntry()
• E getFirst() • [Link]<K,V> pollFirstEntry()
• E getLast() • [Link]<K,V> pollLastEntry()
• E removeFirst() • V putFirst(K k, V v)
• E removeLast() • V putLast(K k, V v)
• SequencedCollection<E> reversed() • SequencedSet<K> sequencedKeySet()
• SequencedCollection<V> sequencedValues()
• SequencedSet<[Link]<K,V>>
sequencedEntrySet()
extends • SequencedMap<K,V> reversed();
SequencedSet
• SequencedSet<E> reversed()
Now, lets do the same operations using Java 21
Collection Predictable First & Last Part of
Iteration Element operation
List<String> list = new ArrayList<>([Link]("B", "C", "D")); Sequenced
List Follows Insertion Collection
Access:
Order
[Link](); //B
[Link](); //D
Add:
[Link]("A"); //A, B, C, D
[Link]("Z"); //A, B, C, D, Z
Remove:
[Link](); //B, C, D, Z
[Link](); //B, C, D
Reverse:
[Link](); //D, C, B
Deque<String> deque= new ArrayDeque<>([Link]("B", "C",
Deque Follows Insertion "D")); Sequenced
Collection
Order
Access:
[Link](); //B
[Link](); //D
Add:
[Link]("A"); //A, B, C, D
[Link]("Z"); //A, B, C, D, Z
Remove:
[Link](); //B, C, D, Z
[Link](); //B, C, D
Reverse:
[Link](); //D, C, B
LinkedHashSet Follows Insertion SequencedSet<String> set= new LinkedHashSet<>([Link]("B",
Order "C", "D"));
Access: Sequence
[Link](); //B
[Link](); //D dSet
Add:
[Link]("A"); //A, B, C, D
[Link]("Z"); //A, B, C, D, Z
//as Set do not contains duplicate, so when we try to insert the duplicate
value, it will find the existing value and shift it to the new place.
[Link]("C"); //C, A, B, D, Z
Remove:
[Link](); //A, B, D, Z
[Link](); //A, B, D
Reverse:
[Link](); //D, B, A
SequencedSet<Integer> sortedSet= new TreeSet<>([Link](14, 5,
7));
SortedSet Follows Sorted Order
Access: Sequence
[Link](); //5
[Link](); //14
dSet
Add:
//as SortedSet sort the values as when inserted, so addFirst() and
addLast() method, do not make sense, that’s why this method throws
UnsupportedOperationException.
[Link](2); //UnsupportedOperationException
[Link](25); //UnsupportedOperationException
//Instead we can simply use add() method
[Link](2); //2, 5, 7, 14
[Link](25); //2, 5, 7, 14, 25
Remove:
[Link](); //5, 7, 14, 25
[Link](); //5, 7, 14
Reverse:
[Link](); //14, 7, 5
Follows Insertion SequencedMap<Integer, String> map= new LinkedHashMap<>
Order ();
LinkedHashMap [Link](100, "B"); Sequence
[Link](200, "C");
[Link](300, "D"); dMap
Access:
[Link](); //100=B
[Link](); //300=D
Add:
[Link](400, "A"); //400=A , 100=B, 200=C, 300=D
[Link](500, "Z"); //400=A , 100=B, 200=C, 300=D,
500=Z
Remove:
[Link](); //100=B, 200=C, 300=D, 500=Z
[Link](); //100=B, 200=C, 300=D
Reverse:
[Link](); //300=D, 200=C, 100=B
SortedMap SortedMap, sort the order based on keys
Follows Sorted Order
SequencedMap<Integer, String> sortedMap= new TreeMap<>(); Sequence
[Link](100, "B"); dMap
[Link](200, "C");
[Link](300, "D");
Access:
[Link](); //100=B
[Link](); //300=D
Add:
//as SortedMap sort the values as when inserted, so putFirst() and
putLast() method, do not make sense, that’s why this method throws
UnsupportedOperationException.
[Link](50, "A");
//UnsupportedOperationException
[Link](400, "Z");
//UnsupportedOperationException
//Instead we can simply use put() method
[Link](50, "A"); //50=A , 100=B, 200=C, 300=D
[Link](400, "Z"); //50=A , 100=B, 200=C, 300=D,
400=Z
Remove:
[Link](); //100=B, 200=C, 300=D, 400=Z
[Link](); //100=B, 200=C, 300=D
Reverse:
[Link](); //300=D, 200=C, 100=B