Properly removing an Integer from a ListInteger
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Removing a value from List<Integer> in Java is a classic source of bugs because List has two different remove methods. One removes by index, and the other removes by object value. Since Integer and int are closely related through autoboxing, it is easy to call the wrong overload and delete the wrong element.
Understand the Two remove Overloads
List defines:
- '
remove(int index)' - '
remove(Object o)'
With List<Integer>, this matters a lot.
This removed the element at index 1, which was 20. It did not remove the integer value 1.
Remove by Value with Integer.valueOf
If you want to remove the number itself, force the Object overload explicitly:
Integer.valueOf(20) creates an Integer object, so Java selects remove(Object) instead of remove(int).
That is the safest and most explicit fix for the overload confusion.
Remove All Matching Integers
If you want to remove every occurrence of a value, removeIf is cleaner than repeated single removals.
This is especially helpful when the list may contain duplicates.
Remove While Iterating Safely
If you are scanning the list and conditionally removing values, use an iterator rather than modifying the list inside a for-each loop.
That avoids ConcurrentModificationException.
Why Autoboxing Causes Confusion
Java automatically converts between int and Integer when needed. That is convenient, but in overloaded methods it can hide your intent.
For example:
Many people read that as "remove the value 1." Java reads it as "remove the element at index 1."
The ambiguity disappears when you write:
That one line communicates the intent unambiguously to both Java and the next person reading the code.
If you are reviewing older code and see remove(someNumber), always pause and ask whether someNumber represents a position or a value. On List<Integer>, that difference is not cosmetic. It changes which overload the compiler picks and therefore which element disappears.
Common Pitfalls
The biggest mistake is assuming list.remove(1) removes the integer 1. On List<Integer>, it removes the element at index 1.
Another problem is using remove(Object) repeatedly in a loop without realizing it only removes the first matching occurrence each time.
Some developers also try to remove from a list inside a for-each loop, which can throw ConcurrentModificationException. Use removeIf or an iterator instead.
Finally, remember that List.of(...) creates an immutable list. If you want to remove anything, wrap it in a mutable implementation such as new ArrayList<>(List.of(...)).
Summary
- '
List<Integer>has two differentremoveoverloads: by index and by object value.' - '
list.remove(1)removes index1, not the integer value1.' - Use
list.remove(Integer.valueOf(1))when you mean value-based removal. - Use
removeIfto delete all matching values. - Use an iterator when removing conditionally during iteration.
Related reading
- Property set method not found error during reflection
- Proposing an algorithm for arbitrary shape Bit Matrix Transposition with BDD-like structure
- Prove NP-Completeness clique independent set graph
- prove the algorithm that uses min-heap to merge k sorted lists
- Property 'security.basic.enabled' is Deprecated The security auto-configuration is no longer customizable
- Property 'spring.profiles.active' imported from location 'class path resource application-dev.yml' is invalid
- Proving that a two-pointer approach works pair sum
- Pseudocode to compare two trees

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.