Quick revision
The short answer
For values the library owns, methods should return unmodifiable copies such as List.copyOf, so callers can never change internal state and the library stays free to change its implementation. Views avoid copying but expose later changes, so they are acceptable only when documented as live views; mutable return values let callers come to depend on mutability, which becomes a permanent compatibility burden. Parameters should accept any Collection and be copied defensively if stored, and since copies cost allocation, hot paths are measured and data is built immutable once rather than copied repeatedly.
What to remember
In this answer, we compared returning internal lists, views and copies. The standard I would set is unmodifiable copies such as List.copyOf for values the library owns, documented views only when live data is genuinely wanted, and no mutable returns, since callers would come to depend on mutability. Parameters accept any collection and are copied if stored. To keep the cost low, we build immutable data once and return the same instance, and we measure hot paths before relaxing the default.
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.
For values the library owns, methods should return unmodifiable copies such as List.copyOf, so callers can never change internal state and the library stays free to change its implementation. Views avoid copying but expose later changes, so they are acceptable only when documented as live views; mutable return values let callers come to depend on mutability, which becomes a permanent compatibility burden. Parameters should accept any Collection and be copied defensively if stored, and since copies cost allocation, hot paths are measured and data is built immutable once rather than copied repeatedly.
Why this question is asked
How a shared library returns collections looks like a detail of style, but it decides who can change the library's internal state, what callers can rely on, and how much garbage every call creates. Once a library is used by many teams, the choice is very hard to undo: any change in mutability breaks somebody. Interviewers ask this design question to see whether we can weigh safety, compatibility and performance and turn them into a clear standard. In this answer, we will compare the three options with code, look at their costs, set the rule for parameters, and finish with how to keep the performance cost low.
1The Three Options
A method returning a collection can hand out its internal mutable list directly, wrap it in a read-only view with Collections.unmodifiableList, or return an unmodifiable copy with List.copyOf. The three look similar to the caller, since all are typed as List, but behave very differently.
| Option | Caller can modify? | Sees later changes? | Cost per call |
|---|---|---|---|
| Internal mutable list | Yes, internal state | Yes | None |
Mutable copy (new ArrayList) | Yes, own copy | No | Copy |
| Unmodifiable view | No | Yes, live | Small wrapper |
Unmodifiable copy (List.copyOf) | No | No, snapshot | Copy, or none if already immutable |
Notice that raw() lets any caller bypass the class's rules. If Team must validate members or keep a size limit, a caller adding through raw() breaks that silently. The view prevents changes through the returned reference but still shows later internal changes. The copy is a snapshot that never changes.
2The Default: Unmodifiable Copies for Owned Values
For data the library owns, the standard I would set is an unmodifiable copy. It gives the strongest safety: callers cannot change our state, and we cannot surprise them by changing theirs later. It also makes the result safe to share between threads and to cache, as long as the elements are immutable.
Just as important is what it does for compatibility. Because the result cannot be modified, no caller can write code that depends on modifying it. That keeps us free to change how we compute or store the data in a later version. With immutable data the copy is often free: if the internal list is itself an unmodifiable List.copyOf result, calling List.copyOf on it returns the same instance without copying.
3When Views Are Acceptable, and Why Mutable Returns Are Not
Views have a real use. A view over a list of a million elements costs nothing to create, while a copy costs a million references. And sometimes the caller genuinely wants live data, such as a registry that should show listeners added later. In those cases a view is fine, but the method must say so in its name or Javadoc, for example liveMembers(), because a caller who expects a snapshot can be surprised when the content changes during iteration, or even get a ConcurrentModificationException if another thread changes the list.
Mutable return values are the option I would rule out. Returning the internal list breaks encapsulation outright. Returning a fresh mutable copy is safe for our state, but it invites callers to modify the result, and once they do, we can never switch to an unmodifiable return or a shared cached value without breaking them, even though the signature stays List. The compatibility burden is permanent, and it is invisible to the compiler.
4Parameters: Accept Broadly, Copy If Stored
For parameters, the rule is the mirror image. Accept the most general type that works, often Collection<? extends T> or List<? extends T>, so callers can pass whatever they have without converting. Never modify the argument unless the method's contract says so. And if the method keeps the collection beyond the call, for example in a field, copy it, because the caller may still change it afterwards.
Copying on the way in and on the way out may look like double work, but each copy protects a different boundary: the first protects us from the caller, the second protects us from callers of our getters. For immutable data, both can be the same object.
5Keeping the Performance Cost Low
Copies cost allocation, and on a hot path called thousands of times per second, a copy of a large list on every call can show up in profiles. The answer is not to give up safety, but to arrange the data so copies are rare. The best pattern is to build immutable data once: when the internal state changes, create a new unmodifiable list and store it; every getter then returns that same instance with no copy at all.
This works well when reads far outnumber writes, which is the common case for configuration, registries and reference data. Where writes are frequent and lists are large, a documented view or a different API, such as a method that takes a visitor or returns a Stream, may be better. Either way, the standard should say to measure with an allocation profiler before relaxing the default, and to fix only the paths that the profile shows matter.
6Follow-up questions
Should methods return Stream instead of List?
For large or lazily computed results, returning a Stream avoids building a collection at all, and it is naturally read-only. But a Stream can only be consumed once and is awkward for callers who need size or random access. I would use Stream for large or computed sequences and List for ordinary results.
Is it acceptable to return null for no results?
No. Return an empty collection, such as List.of(). It costs nothing, and callers can iterate without a null check. Returning null for an empty result is a common source of NullPointerExceptions.
What about collections of mutable objects?
An unmodifiable list only freezes the list structure. If its elements are mutable, callers can still change them, so for full safety the elements should be immutable too, ideally records of immutable values.
7Conclusion
In this answer, we compared returning internal lists, views and copies. The standard I would set is unmodifiable copies such as List.copyOf for values the library owns, documented views only when live data is genuinely wanted, and no mutable returns, since callers would come to depend on mutability. Parameters accept any collection and are copied if stored. To keep the cost low, we build immutable data once and return the same instance, and we measure hot paths before relaxing the default.
Open with
My standard would be that shared library methods return unmodifiable copies, usually List.copyOf, for any data the library owns. That gives callers a snapshot they cannot change, so our internal state is protected, the result is safe to share between threads, and, importantly for compatibility, no caller can ever start depending on modifying it, which keeps us free to change the implementation.
Then, if they want more
Views from Collections.unmodifiableList are cheaper because they do not copy, but they reflect later changes, so I would allow them only when live data is the point, and the method name or Javadoc must say it is a view. I would not allow mutable return values.
Returning the internal list breaks encapsulation, and returning a mutable copy invites callers to modify it; then switching to an unmodifiable list later breaks them with no compile error. For parameters, accept the most general type, like Collection of extends T, never modify inputs unless the contract says so, and copy them if they are stored.
On performance, copies cost allocation, so the pattern I recommend is building immutable data once when the state changes, so getters return the same instance with no copy; List.copyOf of an already immutable list also returns the same instance. If profiling shows a hot path still suffers, we can use a documented view or a Stream there.
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 "You are setting coding standards for returning collections from shared library methods....": https://scrivio-interview-library.netlify.app/java/collections-overview/6d269f0cc9/