Java / Java 21 Collection Framework features Interview questions
What happens when you modify the original collection after calling reversed() on it?
Because reversed() returns a live view rather than a copy, any structural change to the original mutable collection - adding, removing, or replacing elements - is immediately visible through the reversed view too.
List<String> list = new ArrayList<>(List.of("a", "b")); List<String> rev = list.reversed(); // [b, a] list.add("c"); System.out.println(rev); // [c, b, a]
This mirrors how other views in the collections framework behave - similar to how keySet() or Collections.unmodifiableList() stay linked to their backing collection rather than freezing a snapshot at creation time.
If code depends on a stable, unchanging reversed snapshot, it should copy the view into a new collection immediately: new ArrayList<>(list.reversed()).
More Related questions...