Sequenced Collections - Deep Dive with the Expert

Java · 17K views · 2025 · 45:19

Open on YouTube

Programming language designSoftware practice, open source & careerstalk

Why Stack Overflow recommends it

Stack Overflow users link it as the reference explanation of Java 21's SequencedCollection and its reversed() method, mostly in answers about reversing lists and map ordering.

Recommended by 1 people across 2 different months, 2023 to 2026.

What it is

A 45-minute JavaOne 2025 conference talk by Stuart Marks of Oracle's Java Platform Group on the Sequenced Collections feature added in JDK 21 (JEP 431). It covers the new SequencedCollection, SequencedSet and SequencedMap interfaces, operations at either end of a collection, reversed views, and LinkedHashMap improvements.

What people said

Recommends Basil Bourque · 2025-10 · Stack Overflow · on “Collections.reverse() vs Lists.reverse() which one is faster?”

tl;dr In Java 21+, get a view onto your list in reverse order by calling java.util.List#reversed [LINK] ( very fast as no re-ordering is actually performed): myList.reversed() The above has the same effect as Google Guava com.google.common.collect.Lists.reverse() [LINK] . For a reversed unmodifiable list [LINK] that is separate from the original (not a view), the simplest and possibly fastest is a one-liner: List.copyOf( new ArrayList <>( myList ) ).reversed() List#reversed Java 21 brought sequenced collections [LINK] ( video [this video] ). ArrayList , List , etc. now inherit from SequencedCollection [LINK] . That interface requires a method reversed [LINK] . That method returns a reverse-ordere

Recommends Basil Bourque · 2026-01 · Stack Overflow · on “Is there a more succinct way to convert a String to a Set of Characters in Java?”

tl;dr No need to define your hexadecimal digits. If your goal is merely validating text input as hexadecimal digits, write a one-liner to check that each code point represents a hexadecimal base-16 [LINK] digit by passing 16 to Character.digit [LINK] . boolean allHexDigits = inputString .codePoints() .allMatch( codePoint -> Character.digit( codePoint , 16 ) >= 0 // Test if code point represents a character that is a hexadecimal (Base 16) digit. ) ; Or if you insist on using char type, use solution from Comment by Venkata Raju [LINK] : boolean isHexDigit = ( ch <= 'f' ) && ( Character.digit( myChar , 16 ) >= 0 ) ; Avoid char Beware: The char type (and its wrapper class Character ) has been es

Cites ZhekaKozlov · 2023-04 · Stack Overflow · on “How to get a reversed list view on a list in Java?”

SequencedCollection#reversed Since Java 21 there is a new interface SequencedCollection [LINK] (which is a superinteface for List ). It has the method reversed() [LINK] which provides a reverse-ordered view of a collection: jshell> List.of(1, 2, 3, 4, 5).reversed() $1 ==> [5, 4, 3, 2, 1] See JEP 431 [LINK] and video [this video] .

Cites M. Justin · 2025-09 · Stack Overflow · on “Why does LinkedHashMap not implement SortedMap?”

SequencedMap Java 21 introduced the SequencedMap [LINK] interface, which perhaps makes the reasoning and difference more explicit. Per its Javadocs, SequencedMap is "a Map that has a well-defined encounter order." The term "encounter order" is defined in the Javadocs for SequencedCollection [LINK] : The elements of a sequenced collection have an encounter order , where conceptually the elements have a linear arrangement from the first element to the last element. Given any two elements, one element is either before (closer to the first element) or after (closer to the last element) the other element. SortedMap SortedMap [LINK] , on the other hand, imposes a total ordering on its elements: A

Cites Basil Bourque · 2025-10 · Stack Overflow · on “Sort objects in ArrayList by date?”

tl;dr Sort the List by specifying a Comparator . record Employee ( String name , LocalDate hired ) {} List < Employee > employees = new ArrayList<>() ; … employees.sort ( Comparator .comparing( Employee :: hired ) .thenComparing( Employee :: name ); ); Or, build the Comparator into the class, implementing Comparable , then tell the List to sort by natural order. public record Employee( String name , LocalDate hired ) implements Comparable < Employee > { private static final Comparator < Employee > COMPARATOR = Comparator .comparing( Employee :: hired ) .thenComparing( Employee :: name ); @Override public int compareTo ( final Employee other ) { return Employee.COMPARATOR.compare( this , othe

Cites Basil Bourque · 2025-10 · Stack Overflow · on “How to sort a List/ArrayList?”

tl;dr In modern Java: List // Literals syntax, instead of multiple `.add` calls. .of( 0.5 , 0.2 , 0.9 , 0.1 , 0.1 , 0.1 , 0.54 , 0.71 , 0.71 , 0.71 , 0.92 , 0.12 , 0.65 , 0.34 , 0.62 ) .stream() // Auto-boxing in line above produced `Double` objects, here fed into a stream as `Double` object elements. .sorted() // Elements of stream are sorted using natural-order. .toList() // Returns an unmodifiable `List` object of an unspecified `List` implementation class. .reversed() // For descending order, return a view onto that collection, in reverse order. Does *not* actually create a new collection, so very fast. .forEach( IO :: println ); // Streams our list of `Double` objects, passing each to t