Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Many Java APIs return views, such as subList, keySet, Arrays.asList and unmodifiable wrappers, rather than copies. What is the long-term cost of view-based APIs, and when would you allow them in your organisation's shared libraries?

Views avoid copying, but they tie the returned object's lifetime and behaviour to its source: later changes to the source surprise callers or break the view, the view keeps large sources alive in memory, and the contract becomes harder to explain and to change. In an organisation's shared libraries we would default to returning copies or immutable values, and allow views only where measurements show the copy really costs too much, with the method named and documented as a view.

Why this question is asked

API return types look like a small decision, but in a shared library they become contracts that dozens of teams build on for years. The JDK's views, subList, keySet, Arrays.asList and the unmodifiable wrappers, show both the power of views and the bugs they cause. Interviewers ask senior candidates this to hear how they weigh a performance benefit against long-term cost, and how they turn that judgement into a rule people can follow. In this answer, we will recap what a view is, look at its three long-term costs with examples, compare it with copies, and then set the policy we would adopt and how we would apply it.

1A Quick Recap: Views Versus Copies

A copy is a new, independent collection holding the same elements at the moment of the call. A view is an object that presents data stored elsewhere: reading it reads the source, and if it is writable, writing it writes the source. Collections.unmodifiableList is a read-only view: callers cannot change it, but they still see every change the owner makes.

The appeal of views is cost. Creating one is O(1) whatever the size of the source, while a copy is O(n) in time and memory. For a method that returns part of a ten-million-element structure many times per second, that difference can be real.

Java
List<String> source = new ArrayList<>(List.of("a", "b", "c"));
List<String> view = Collections.unmodifiableList(source);
List<String> copy = List.copyOf(source);
source.add("d");
System.out.println(view); // [a, b, c, d]: the view follows the source
System.out.println(copy); // [a, b, c]: the copy is a snapshot

2Cost 1: Behaviour Tied to the Source

A caller who receives a view gets an object whose contents can change under them, or which stops working. A subList becomes invalid after the source is structurally modified and throws ConcurrentModificationException on its next use, often in code far from the change. An unmodifiable view looks safe to share, but a caller that iterates it while the owner adds elements on another thread can fail or read inconsistent data, because the view adds no locking.

These bugs are expensive because the failure appears in the caller's code while the cause is in the library's code, sometimes much later. The library author knows it is a view; the caller usually does not.

3Cost 2: Retained Memory

A view holds a reference to its whole source. A caller that caches a 100-element subList of a million-element list keeps the million elements alive; a caller that stores map.keySet() keeps the entire map, values included. Nobody wrote a leak deliberately, but the heap grows. In a shared library, one view-returning method can create this leak in every service that uses it.

4Cost 3: Contracts That Are Hard to Explain and to Change

A view's contract has many parts: does it reflect later changes, can it be modified, does modifying it change the source, when does it become invalid, is it thread-safe, does it keep the source alive. Most Javadoc covers one or two of these, and callers come to rely on the rest by accident.

Worse, once callers depend on live updates, the library cannot switch to returning a copy without breaking them, and once callers depend on a snapshot, it cannot switch to a view. Returning a view therefore narrows the library's future choices. A copy, especially an immutable one, has a one-sentence contract: here is the data as of the call, and nobody can change it.

Returning a view versus a copy
AspectViewImmutable copy
Cost of the callO(1)O(n) time and memory
Later source changesvisible, or the view breaksnot visible
Memory retained by callerthe whole sourceonly the elements
Thread safety for callerdepends on the sourcesafe to share
Contract to documentsix questionsone sentence
Freedom to change laterlowhigh
A view saves the copy and pays in coupling.

5The Policy We Would Set

Our default for shared libraries is to return copies or immutable values. Collections returned from public methods are List.copyOf, Set.copyOf or Map.copyOf, or records holding such copies. Their cost is usually small: copying a list of a few hundred references takes microseconds. And List.copyOf is free when the input is already an immutable list of the same kind, because it returns the same instance, so a library that keeps immutable state internally pays nothing.

Java
public final class Catalog {
private volatile List<Product> products = List.of(); // immutable snapshot
public List<Product> products() {
return products; // already immutable: safe to return, no copy needed
}
public void replace(Collection<Product> fresh) {
products = List.copyOf(fresh); // copy once on write, share freely on read
}
}
Step throughDeciding whether a shared API may return a view
  1. Is the result a collection or part of one?
    YesDefault: return an immutable copyNoNo collection policy needed
  2. Does a profile show the copy matters?
    YesView allowed with conditionsNoKeep the immutable copy
  3. Name it as a viewkeysView(), rangeView()
  4. Document its behaviourlive updates, writability, invalidation, retention

4 steps in order. Play them, or step through them one at a time.

Views are allowed as an exception, under three conditions. First, a measurement shows the copy matters, for example a profile where copying dominates a hot path, not a guess. Second, the method's name says it is a view, such as keysView() or rangeView(from, to), so callers cannot miss it. Third, the Javadoc answers the questions above: whether it reflects changes, whether it can be modified, when it becomes invalid, and what it retains.

6Applying It Across Teams

A policy only works if it is easy to follow. We would add it to the library design guidelines with the reasoning, not only the rule, provide small helpers so returning an immutable copy is the easiest thing to write, and add a review checklist item for public methods that return collections. Static analysis or ArchUnit rules can flag public methods returning subList, keySet or unmodifiableList results directly.

For existing view-returning methods, we do not change behaviour silently, because callers may rely on live updates. We add a copying alternative, deprecate the view method with an explanation, and migrate callers over a release or two. As an example, a team found that Inventory.itemsFor(warehouse) returned an unmodifiableList view; two services had cached it and were holding the full inventory map in memory. We added itemsSnapshot(warehouse) returning List.copyOf, moved both services, and deprecated the old method with a note on the retention problem.

7Follow-up questions

Is Collections.unmodifiableList a copy?

No, it is a read-only view. Callers cannot modify it, but they see every change made to the underlying list, and it keeps that list alive. List.copyOf, List.of and Stream.toList() return true immutable lists that do not change after creation.

Do copies hurt performance in high-throughput services?

Rarely in a way that matters. Copying a few hundred references is cheap compared with network calls, serialisation or database access in the same request. Where it does matter, keeping internal state immutable lets us return the same object without copying at all, which beats both copies and views.

Are there views the JDK gets right that we should copy?

Read-only views of immutable data are harmless, since nothing can change underneath them. Iteration views such as streams are also fine, because they are consumed at once and not stored. The problems come from long-lived views over mutable sources.

8Conclusion

In this answer, we saw that views save the O(n) copy but cost in three ways: their behaviour depends on later changes to the source, they keep the whole source reachable, and their contracts are hard to explain and to change. The policy we would set for shared libraries is to return immutable copies by default, keep internal state immutable so returning it is free, and allow views only where measurements justify them, with names and documentation that make them obvious.

What to say in the interviewAbout 90 seconds aloud

Open with

Views like subList, keySet, Arrays.asList and unmodifiableList are attractive because they cost nothing to create, while a copy is linear. The long-term cost is coupling. First, behaviour: the caller's object changes when the source changes, or stops working, like a subList throwing ConcurrentModificationException after a structural change, and an unmodifiable view is not thread-safe if the owner keeps modifying the source.

Then, if they want more

The bug appears in the caller's code, far from the cause. Second, memory: a view keeps its whole source reachable, so a cached subList of a hundred elements can retain a million.

Third, the contract: a view raises six questions about live updates, writability, invalidation, thread safety and retention, callers come to depend on whatever it does, and then we cannot switch to a copy or back without breaking someone.

So in shared libraries my default is immutable copies, List.copyOf or records holding them, ideally with immutable internal state so returning it costs nothing, since List.copyOf on an immutable list returns the same instance.

I would allow a view only when a profile shows the copy matters, with a name like keysView so nobody mistakes it, and Javadoc that answers those questions. For existing view methods, I would add a snapshot alternative, deprecate the view with the reason, and migrate callers, rather than silently changing behaviour.

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.
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 "Many Java APIs return views, such as subList, keySet, Arrays.asList and unmodifiable...": https://scrivio-interview-library.netlify.app/java/lists/80817a8193/