Java / Java 21 Collection Framework features Interview questions
What happens when you call reversed() on an immutable List returned by List.of()?
It works fine and returns a reverse-ordered view - reversed() is a read operation, so it's fully supported even on immutable lists, since flipping the read direction doesn't require modifying the underlying list.
List<Integer> frozen = List.of(1, 2, 3); List<Integer> rev = frozen.reversed(); // [3, 2, 1], also immutable
The returned view is itself immutable too - calling a mutating method like addFirst() on the reversed view throws UnsupportedOperationException, exactly as it would on the original List.of(...) instance.
So the immutability of the source list carries through to its reversed view; you get a read-only "reverse lens" on the same unmodifiable data, not a way to sneak around the immutability.
More Related questions...