Quick revision
The short answer
The compiler gives each use of List<?> its own fresh captured type, so it cannot prove that the value list.get(1) returns has the type list.set(0, ...) expects, even though it is the same list. The fix is to keep the simple List<?> signature publicly and delegate to a private generic helper, private static <T> void swapHelper(List<T> list), where inference captures the wildcard as one named type T and both calls agree. The JDK's Collections.swap has the same List<?> signature, but it sidesteps the problem internally with a raw type rather than a helper.
What to remember
In this answer, we saw that list.set(0, list.get(1)) fails on a List<?> because the compiler captures the wildcard separately for each use, producing two unrelated types, CAP#1 and CAP#2. A private generic helper, <T> void swapHelper(List<T> list), captures the wildcard once as T, so reads and writes agree, while the public method keeps its List<?> signature. Collections.swap shares that public signature but uses a raw type internally; for our own code the helper is cleaner because it needs no suppressed warnings.
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.
The compiler gives each use of List<?> its own fresh captured type, so it cannot prove that the value list.get(1) returns has the type list.set(0, ...) expects, even though it is the same list. The fix is to keep the simple List<?> signature publicly and delegate to a private generic helper, private static <T> void swapHelper(List<T> list), where inference captures the wildcard as one named type T and both calls agree. The JDK's Collections.swap has the same List<?> signature, but it sidesteps the problem internally with a raw type rather than a helper.
Why this question is asked
This compile error surprises almost everyone the first time: we take an element out of a list and try to put it back into the same list, and the compiler says the types do not match. Understanding why reveals how the compiler actually handles wildcards, and the fix is a small technique worth knowing by name. Interviewers ask it to see whether we can reason about capture conversion rather than reaching for a cast. In this answer, we will recap what List<?> means, reproduce the error, explain capture conversion, fix it with a helper, and compare with what the JDK does.
1A Quick Recap of List<?>
List<?> is a list of some specific type that we do not know. It could be a List<String>, a List<Integer> or anything else. That is different from List<Object>, which is a list that may hold anything. Because the element type is unknown, reading gives us an Object, and adding is forbidden except for null, since we cannot know whether a value fits.
Swapping two elements does not add anything foreign: it only moves values that are already in the list. So intuitively it should be safe. The question is why the compiler cannot see that.
2Reproducing the Error
Here is the helper from the question. It does not compile.
The names CAP#1 and CAP#2 are the clue. They are types the compiler invented, one for each time it looked at the expression list. To the compiler, the list in list.set and the list in list.get have element types CAP#1 and CAP#2, two unknown types with no known relationship.
3Why Each ? Becomes a Fresh Type
Whenever an expression has a wildcard type, the compiler performs capture conversion: it replaces the ? with a new, unnamed type variable that stands for whatever the real type is. That lets it type-check method calls, because List<CAP#1> has a concrete-looking set(int, CAP#1) and get(int) returning CAP#1.
The catch is that capture happens separately for each use of the expression. The compiler does not track that two uses of the variable list refer to the same object, or that the object's type cannot change between them. So list.get(1) returns a CAP#2, and list.set(0, ...) expects a CAP#1. As far as the type system knows, CAP#2 might be String and CAP#1 might be Integer. Passing one where the other is required would be unsafe in general, so it is rejected.
Notice the compiler is not wrong, just limited. It reasons about types, not about object identity, and the type List<?> really does allow two different lists to have different element types.
- Capture list for set()List<CAP#1>: set expects a CAP#1
- Capture list for get()List<CAP#2>: get returns a CAP#2
- Is CAP#2 a subtype of CAP#1?YesCompilesNoError: argument mismatch
3 steps in order. Play them, or step through them one at a time.
Two captures of the same variable are two unrelated unknown types.
4The Fix: A Private Generic Helper
The trick is to capture the wildcard once and give it a name. A generic method parameter of type List<T> can accept a List<?>: when we call it, inference captures the wildcard and binds T to that one captured type. Inside the helper, every use of list has the same element type T.
Notice that the helper also does a proper swap, using a temporary variable, while the original line only copied element 1 over element 0. The public method keeps the simpler List<?> signature, which tells callers the method works with any list without making them think about type parameters. The private helper exists only to give the compiler a name for the captured type. This pattern is called a capture helper, and it is the standard way to write through a wildcard when we only move values that came from the same list.
5What the JDK Does in Collections.swap
Collections.swap has exactly this signature, public static void swap(List<?> list, int i, int j). Its implementation, however, does not use a helper. It assigns the list to a raw List variable and calls set on that, with the unchecked warning suppressed. The source carries a comment noting that a private capture helper would also work, and the JDK simply chose the shorter raw form. Both are safe here, because the method only moves values within one list.
For our own code, the helper is the better choice. It keeps full type checking in the method body, needs no @SuppressWarnings, and makes the intent obvious to the next reader.
6Follow-up questions
Why not just declare the public method as <T> void swapFirstTwo(List<T> list)?
That works too. The List<?> form is preferred for public APIs when the type parameter would appear only once, because it is simpler for callers to read. The helper gives us the simple public signature and the named type internally.
Would a cast fix it?
A cast such as ((List<Object>) list).set(0, list.get(1)) compiles with an unchecked warning and happens to be safe here, but it switches off checking for the whole expression. The capture helper gets the same result with no warning and no possibility of a mistake slipping through.
When does capture conversion happen without us noticing?
Every time we call a method on an expression of wildcard type. Usually it is invisible, as with list.get(0) or list.size(). It only becomes visible when two captured types have to agree, as in this swap, or in error messages that mention CAP#1.
7Conclusion
In this answer, we saw that list.set(0, list.get(1)) fails on a List<?> because the compiler captures the wildcard separately for each use, producing two unrelated types, CAP#1 and CAP#2. A private generic helper, <T> void swapHelper(List<T> list), captures the wildcard once as T, so reads and writes agree, while the public method keeps its List<?> signature. Collections.swap shares that public signature but uses a raw type internally; for our own code the helper is cleaner because it needs no suppressed warnings.
Open with
The problem is capture conversion. When the compiler sees an expression of type List<?>, it replaces the wildcard with a fresh, unnamed type, and it does that separately each time.
Then, if they want more
So in list.set(0, list.get(1)), the list being set has element type CAP#1 and the list being read has CAP#2. The compiler does not track that it is the same object, so it cannot prove CAP#2 is CAP#1, and the error says something like CAP#2 cannot be converted to CAP#1. The fix is a capture helper.
I keep the public method as swapFirstTwo(List<?> list) and have it call a private generic method, private static <T> void swapHelper(List<T> list), which does T tmp = list.get(0), list.set(0, list.get(1)), list.set(1, tmp).
When I pass the List<?> to the helper, inference captures the wildcard once and binds it to T, so inside the helper reads and writes share one type and everything type-checks without casts or warnings. The public signature stays simple for callers.
For comparison, Collections.swap in the JDK has the same List<?> signature, but internally it assigns the list to a raw List and suppresses the warning, with a comment saying a helper would also work. For my own code I prefer the helper, because it keeps full type checking.
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 Java Language Specification, Java SE 21, Java SE 21 API 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 helper fails to compile: static void swapFirstTwo(List<?> list) { list.set(0...": https://scrivio-interview-library.netlify.app/java/generics/4bd35acd61/