← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 11 of 12

Java Sequenced Collections

Sequenced Collections, added in Java 21, give List, Deque, and LinkedHashSet/Map a uniform way to access the first and last elements via getFirst, getLast, addFirst, addLast, and 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?

java
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
	}
}

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

java
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);
	}
}

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

java
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());
	}
}

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

java
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
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.