Java Sequenced Collections
In this page:
What are Sequenced Collections?
Sequenced Collections, added in Java 21, introduce a uniform way to access the first and last elements of an ordered collection -- methods like getFirst, getLast, addFirst, and addLast now work consistently across List, Deque, and LinkedHashSet.
Example: What are Sequenced Collections?
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
System.out.println(list.getFirst() + " " + list.getLast()); // uniform across List, Deque, LinkedHashSet
}
}
Login to try C/C++/Java/PHP code in the editor
SequencedCollection Interface
SequencedCollection is a new interface that List and Deque now extend, defining a consistent set of methods -- getFirst, getLast, addFirst, addLast, removeFirst, removeLast, and reversed -- for any collection with a well-defined encounter order.
Example: SequencedCollection Interface
import java.util.*;
public class Main {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>(List.of("a", "b", "c"));
deque.addFirst("start");
deque.addLast("end");
System.out.println(deque);
}
}
Login to try C/C++/Java/PHP code in the editor
SequencedSet and SequencedMap
SequencedSet extends both Set and SequencedCollection, implemented by LinkedHashSet and TreeSet, while SequencedMap adds firstEntry, lastEntry, and reversed methods to ordered maps like LinkedHashMap and TreeMap.
Example: SequencedSet and SequencedMap
import java.util.*;
public class Main {
public static void main(String[] args) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("a", 1); map.put("b", 2);
System.out.println(map.firstEntry() + " " + map.lastEntry());
}
}
Login to try C/C++/Java/PHP code in the editor
Reversed Views
The reversed() method returns a reverse-ordered view of the original collection rather than a separate copy, meaning changes made to the original collection are immediately visible when iterating the reversed view.
Example: Reversed Views
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
List<Integer> reversed = list.reversed(); // a view, not a copy
list.add(4);
System.out.println(reversed); // sees the update
}
}
Login to try C/C++/Java/PHP code in the editor
Retrofitted Existing Collections
Existing collection classes like ArrayList, ArrayDeque, LinkedHashSet, TreeSet, LinkedHashMap, and TreeMap were all retrofitted to implement the new sequenced interfaces, so this consistent first/last API became available without requiring any new collection types.
Example: Retrofitted Existing Collections
import java.util.*;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>(List.of(3, 1, 2)); // retrofitted, no new collection type needed
System.out.println(set.getFirst() + " " + set.getLast());
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: