Collections: subList is a view onto the parent list

subList does not copy. Writing through the view changes the parent, and structurally changing the parent invalidates the view.

Code
var list = new ArrayList<>(List.of(0, 1, 2, 3, 4, 5));
List<Integer> middle = list.subList(1, 4);

System.out.println("subList(1,4) = " + middle);
middle.set(0, 99);
System.out.println("parent after view write: " + list);

middle.clear();
System.out.println("clearing the view removes from the parent: " + list);
Output
subList(1,4) = [1, 2, 3]
parent after view write: [0, 99, 2, 3, 4, 5]
clearing the view removes from the parent: [0, 4, 5]
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-30