Java
Collections
List
Integer
Programming

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.

Practice algorithms

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.

java
1import java.util.ArrayList;
2import java.util.List;
3
4public class RemoveExample {
5    public static void main(String[] args) {
6        List<Integer> values = new ArrayList<>(List.of(10, 20, 30, 40));
7
8        values.remove(1);
9        System.out.println(values); // [10, 30, 40]
10    }
11}

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:

java
1import java.util.ArrayList;
2import java.util.List;
3
4public class RemoveByValue {
5    public static void main(String[] args) {
6        List<Integer> values = new ArrayList<>(List.of(10, 20, 30, 20));
7
8        values.remove(Integer.valueOf(20));
9        System.out.println(values); // [10, 30, 20]
10    }
11}

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.

java
1import java.util.ArrayList;
2import java.util.List;
3
4public class RemoveAllTwenties {
5    public static void main(String[] args) {
6        List<Integer> values = new ArrayList<>(List.of(10, 20, 30, 20, 40));
7
8        values.removeIf(n -> n.equals(20));
9        System.out.println(values); // [10, 30, 40]
10    }
11}

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.

java
1import java.util.ArrayList;
2import java.util.Iterator;
3import java.util.List;
4
5public class IteratorRemove {
6    public static void main(String[] args) {
7        List<Integer> values = new ArrayList<>(List.of(10, 20, 30, 20));
8
9        Iterator<Integer> it = values.iterator();
10        while (it.hasNext()) {
11            if (it.next().equals(20)) {
12                it.remove();
13            }
14        }
15
16        System.out.println(values); // [10, 30]
17    }
18}

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:

java
List<Integer> values = new ArrayList<>(List.of(1, 2, 3));
values.remove(1);

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:

java
values.remove(Integer.valueOf(1));

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 different remove overloads: by index and by object value.'
  • 'list.remove(1) removes index 1, not the integer value 1.'
  • Use list.remove(Integer.valueOf(1)) when you mean value-based removal.
  • Use removeIf to delete all matching values.
  • Use an iterator when removing conditionally during iteration.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.