Quick revision
The short answer
subList returns a view backed by the original list, not a copy. Adding 99 to all is a structural change made around the view, so the view is no longer valid, and the next time we use it, here when println iterates it, it throws ConcurrentModificationException. If the page must outlive later changes, we copy it with new ArrayList<>(all.subList(0, 10)) or List.copyOf; and we remember that changes made through the view, such as page.clear(), do change the original list.
What to remember
In this answer, we saw that subList returns a view holding a reference to the backing list, its offset and the modCount it saw at creation. A structural change made directly to the backing list, such as all.add(99), makes the next use of the view throw ConcurrentModificationException. Copying with new ArrayList<>(...) or List.copyOf gives an independent page, while set() on the backing list and changes made through the view, such as page.clear(), keep the view valid and do change the original list.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
subList returns a view backed by the original list, not a copy. Adding 99 to all is a structural change made around the view, so the view is no longer valid, and the next time we use it, here when println iterates it, it throws ConcurrentModificationException. If the page must outlive later changes, we copy it with new ArrayList<>(all.subList(0, 10)) or List.copyOf; and we remember that changes made through the view, such as page.clear(), do change the original list.
Why this question is asked
subList looks like it returns a small list of its own, and that is exactly why this bug is so common. The code reads naturally: take the first page, add something to the full list, print the page. Interviewers ask it to check that we understand views, structural modification and fail-fast checks, three ideas that explain a whole family of collection bugs. In this answer, we will recap what a view is, reproduce the exception, see exactly how the view detects the change, fix it, and look at which changes are safe.
1A Quick Recap of Views
A view is an object that presents data stored somewhere else. subList(0, 10) creates a small object that holds a reference to the backing list, an offset (where the view starts) and a size. It stores no elements of its own. Calling page.get(3) simply reads all.get(0 + 3).
A structural modification is any change to the size of a list: add, remove, clear, addAll. Replacing an element with set() is not structural, because the size and positions stay the same. ArrayList counts structural modifications in an internal field called modCount, which increases every time the size changes.
2Reproducing the Exception
Here is the code from the question, with the result of each line.
7 messages in order. Play them, or step through them one at a time.
The add succeeds; the next use of the view fails.
Notice that the exception does not happen at all.add(99). The add is legal and succeeds. The failure comes later, at the first use of the stale view, here inside println, which calls page.toString(), which iterates the view.
3Why the View Refuses to Work
When the view is created, it records the backing list's modCount. Every operation on the view first compares the list's current modCount with the recorded one. If they differ, someone changed the size of the backing list without going through the view, and the view's offset and size may now point at the wrong elements.
Consider what could happen without the check. If someone removed element 0 from all, the view's index 0 would now show what used to be index 1, and the view would silently present different data. If someone cleared all, the view would claim to have ten elements that no longer exist. Rather than return wrong results, the view fails fast. This check is a best-effort bug detector in single-threaded code, not a promise; the documentation says the behaviour of a view is undefined once the backing list is structurally modified other than through the view.
4Fixing It: Copy When the Page Must Outlive Changes
If the page is meant to be a snapshot, an independent list of the first ten elements, we copy it. Then it no longer depends on the backing list.
We pick new ArrayList when the caller needs to change the page, and List.copyOf when it should be read-only, which is the usual case for a page of results. List.copyOf throws NullPointerException if any element is null, so for data that may contain nulls we use the ArrayList copy. Another fix is simply ordering: create the view after the last change to the list, and use it immediately.
5Which Changes Are Safe
Not every change breaks the view. Changes made through the view keep it valid, because the view updates its own size and recorded modCount. And they do change the original list, which surprises people who think of the page as separate.
| Change | Structural? | View afterwards |
|---|---|---|
all.set(i, x) | no | valid, shows the new value |
page.set(i, x) | no | valid, all changes too |
page.add(x), page.clear() | yes, through the view | valid, all changes too |
all.add(x), all.remove(i) | yes, around the view | invalid: next use throws CME |
| copy made first | n/a | independent of all |
So the rule is easy to state: while a view is alive, change the backing list only through the view, or only with non-structural changes such as set(). Any add or remove made directly on the backing list invalidates every view created before it.
6Follow-up questions
Does LinkedList's subList behave the same way?
Yes. LinkedList inherits subList from AbstractList, which uses the same modCount check, so a structural change to the LinkedList invalidates the view in the same way. Lists from List.of cannot be changed at all, so their sublists never become stale.
Is ConcurrentModificationException only about threads?
No. Despite its name, it most often comes from single-threaded code like this example, where one piece of code changes a list while another holds an iterator or a view over it. With threads the check is not even reliable, because a change in another thread may not be visible; real concurrent access needs concurrent collections or locks.
Why does subList return a view at all instead of a copy?
Because a view is free to create, whatever the range size, and it enables range operations on the backing list, such as list.subList(10, 20).clear() to remove a range in one call, or sorting part of a list. The cost is the coupling we saw here, so we use views briefly and copy when we need to keep the result.
7Conclusion
In this answer, we saw that subList returns a view holding a reference to the backing list, its offset and the modCount it saw at creation. A structural change made directly to the backing list, such as all.add(99), makes the next use of the view throw ConcurrentModificationException. Copying with new ArrayList<>(...) or List.copyOf gives an independent page, while set() on the backing list and changes made through the view, such as page.clear(), keep the view valid and do change the original list.
Open with
The key fact is that subList returns a view, not a copy. page holds a reference to all, an offset and a size, and when it is created it records all's modCount, the counter ArrayList increases on every structural change, meaning anything that changes the size. all.add(99) is a structural change made around the view, so modCount moves on.
Then, if they want more
The add itself succeeds. The exception appears on the next use of the view: println calls toString, which iterates page, and the view compares the list's modCount with the one it recorded, sees they differ, and throws ConcurrentModificationException.
It does that on purpose, because after a structural change its offset and size might point at the wrong elements, so failing fast is better than returning wrong data. The fix depends on what page is meant to be.
If it is a snapshot, I copy it: new ArrayList of all.subList(0, 10) if it needs to be modifiable, or List.copyOf for a read-only page, keeping in mind List.copyOf rejects nulls. Or I create the view only after the last change and use it immediately.
I would also point out the other direction: changes through the view go to the original. page.clear() removes those ten elements from all, and set on either side is visible to both without breaking the view, because set is not structural.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "This code throws ConcurrentModificationException:": https://scrivio-interview-library.netlify.app/java/lists/31eca13679/