Java 21: Sequenced collections (getFirst / getLast)

SequencedCollection adds first/last access and reversed() to ordered collections.

Code
List<String> list = new ArrayList<>(List.of("a", "b", "c"));
System.out.println(list.getFirst());
System.out.println(list.getLast());
System.out.println(list.reversed());
Output
a
c
[c, b, a]
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-26