← 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().
Syntax
java
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?

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.* 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);
	}
}

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

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());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling getFirst() or getLast() on an empty collection, which throws a NoSuchElementException.
  2. Thinking reversed() gives an independent copy, when it is a view, so changes in one show up in the other.
  3. Expecting HashSet or HashMap to have getFirst(), when only ordered collections such as LinkedHashSet and TreeSet are sequenced.

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.