Java Sequenced Collections
In this page:
SequencedCollection<Type> collection = new ArrayList<>();
collection.getFirst();
collection.getLast();
collection.reversed();
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.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new ArrayDeque object and store it in deque
Deque<String> deque = new ArrayDeque<>(List.of("a", "b", "c"));
deque.addFirst("start");
deque.addLast("end");
// Print a line to the console
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.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new LinkedHashMap object and store it in map
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("a", 1); map.put("b", 2);
// Print a line to the console
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
- Calling
getFirst()orgetLast()on an empty collection, which throws aNoSuchElementException. - Thinking
reversed()gives an independent copy, when it is a view, so changes in one show up in the other. - Expecting
HashSetorHashMapto havegetFirst(), when only ordered collections such asLinkedHashSetandTreeSetare sequenced.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: