Quick revision
The short answer
List has two overloads, remove(int index) and remove(Object o). An int argument selects remove(int) because overload resolution tries methods without boxing first, so list.remove(value) with an int value removes the element at that position, or throws IndexOutOfBoundsException, instead of removing that value. To remove by value, pass an Integer, list.remove(Integer.valueOf(v)). In our own APIs we avoid overloads that differ only by primitive versus boxed or related types and give the operations distinct names such as removeAt and removeValue.
What to remember
In this answer, we saw that List<Integer>.remove(x) with an int argument calls remove(int index), because overload resolution chooses methods that need no boxing first, so the call removed the element at that position or threw IndexOutOfBoundsException. Passing Integer.valueOf(v), casting to Integer or using removeIf removes by value. The design lesson is that overloads differing only by primitive versus boxed or closely related types are error-prone, and distinct names such as removeAt and removeValue make the intent impossible to misread.
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.
List has two overloads, remove(int index) and remove(Object o). An int argument selects remove(int) because overload resolution tries methods without boxing first, so list.remove(value) with an int value removes the element at that position, or throws IndexOutOfBoundsException, instead of removing that value. To remove by value, pass an Integer, list.remove(Integer.valueOf(v)). In our own APIs we avoid overloads that differ only by primitive versus boxed or related types and give the operations distinct names such as removeAt and removeValue.
Why this question is asked
This is a bug that compiles cleanly, passes a quick test with lucky data and then deletes the wrong record in production. It is also a good example of how an API decision made in Java 1.2, before boxing existed, became a trap once boxing arrived in Java 5. Interviewers ask it to check whether we can connect a surprising behaviour to overload resolution rules, fix it, and draw a design lesson from it. In this answer, we will reproduce the bug, explain why the compiler picks remove(int), fix it in a few ways, and then look at how to design APIs that cannot be misread this way.
1A Quick Recap of the Two remove Methods
java.util.List declares remove(int index), which removes the element at a position and returns it, and remove(Object o), inherited from Collection, which removes the first element equal to o and returns a boolean. For most lists they never collide, because a List<String> argument is clearly an Object and an index is clearly an int. A List<Integer> is the problem: both an index and a value can be written as int.
2Reproducing the Bug
Here is a list of order ids, and code that wants to remove the id 20 from it.
The first call was meant to remove the value 1 but removed 20, the element at position 1. No exception, no warning, just the wrong data. The second call is the lucky case: the value happens to be larger than the list, so it fails loudly. With small ids and long lists, the silent case is the common one.
3Why the Compiler Picks remove(int)
Overload resolution happens at compile time in phases. In phase one, the compiler considers only methods that apply without boxing, unboxing or varargs. With an int argument, remove(int) applies directly, while remove(Object) would need the int to be boxed to Integer first. Because phase one already found an applicable method, the compiler stops there and never considers boxing. The type parameter Integer of the list plays no role; the choice depends only on the argument's static type, int.
- Static type of x is int?YesPhase 1: remove(int) applies, stopNoCheck if x is Integer or Object
- remove(int) runsremoves by position, returns the element
- x is Integer or Objectphase 1:
remove(Object)applies;remove(int)would need unboxing - remove(Object) runsremoves first equal value, returns boolean
4 steps in order. Play them, or step through them one at a time.
This rule exists so that code written before Java 5 kept calling the same methods when boxing was added. The cost is that remove(int) wins every time an int is passed, whatever the programmer meant.
4Fixing the Call
To remove by value, we make the argument's static type Integer or Object, so that phase one finds remove(Object) and not remove(int).
Integer.valueOf(value) is the clearest fix because it states the intent in the call. The cast works the same way but is easier to misread. removeIf with a predicate avoids the overload entirely and also removes all matching elements, not just the first, which is often what the business rule wanted. Notice that inside the lambda id is an Integer and id == 30 unboxes it, so the comparison is by value.
5How API Design Could Have Avoided It
The deeper lesson is about overloads. Overloads whose parameters differ only as primitive versus boxed type, int and Integer, or that take the same number of arguments with related types, such as int and Object, are error-prone, because the reader cannot tell from the call which method runs. The compiler applies precise rules, but people read the code by meaning. Effective Java's advice is to avoid such confusing overloads, and the List.remove case is the standard example.
In our own APIs, distinct names remove the ambiguity: removeAt(int index) and removeValue(T value), or deleteById(long id) and delete(Order order). The newer JDK APIs follow this style: Java 21 added removeFirst() and removeLast() to List through SequencedCollection, with names instead of index overloads. A useful review question for any pair of overloads is whether swapping a primitive for its wrapper at a call site would silently change which method runs. If it would, use different names.
| Risky overload pair | Why it misleads | Safer design |
|---|---|---|
remove(int) / remove(Object) | int picks the index version | removeAt(int) / removeValue(T) |
get(long id) / get(Long key) | boxing changes the method | findById(long) / findByKey(Long) |
set(int, Object) / set(Object, int) | argument order is easy to swap | named methods or a parameter object |
6Follow-up questions
Why did the Java designers not rename remove when boxing arrived?
Renaming or removing remove(int) would have broken every existing program that used it, and the Java platform has always prioritised backward compatibility. The phase rules for overload resolution were designed precisely so that old calls kept their meaning, which means remove(int) still wins for int arguments today.
Does the same problem appear in other collections?
It appears wherever an index or key overload sits next to an Object overload with the same arity. Collection.remove(Object) alone is safe because there is no index version, so a Set<Integer>.remove(5) boxes and removes by value. The trap is specific to List and to similar APIs that mix positions with elements.
7Conclusion
In this answer, we saw that List<Integer>.remove(x) with an int argument calls remove(int index), because overload resolution chooses methods that need no boxing first, so the call removed the element at that position or threw IndexOutOfBoundsException. Passing Integer.valueOf(v), casting to Integer or using removeIf removes by value. The design lesson is that overloads differing only by primitive versus boxed or closely related types are error-prone, and distinct names such as removeAt and removeValue make the intent impossible to misread.
Open with
List has two remove methods: remove of int index, which removes by position and returns the element, and remove of Object, which removes the first equal element and returns a boolean.
Then, if they want more
With a List of Integer, when I pass an int, the compiler picks remove of int, because overload resolution first tries methods that apply without boxing or varargs, and remove of int applies directly, while remove of Object would need boxing.
So list.remove(value) with value equal to 1 silently removes whatever is at index 1, and with a large value it throws IndexOutOfBoundsException. The list's type parameter does not matter; only the static type of the argument does.
To remove by value, I pass an Integer, for example list.remove(Integer.valueOf(v)), or use removeIf with a predicate, which also removes all matching elements and makes the intent obvious. The phase rules exist so that pre-Java 5 code kept its meaning when boxing arrived, which is why the JDK could not fix this.
For my own APIs the lesson is to avoid overloads with the same number of parameters whose types differ only by primitive versus boxed type or are closely related, and to use distinct names like removeAt and removeValue instead.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 5, Java 21
- 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 "A List<Integer> remove call deletes the wrong element: list.remove(index) where index is...": https://scrivio-interview-library.netlify.app/java/oop-principles/dbab03297d/