Properly removing an Integer from a List<Integer>

Here’s a nice pitfall I just encountered.
Consider a list of integers:

List<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(6);
list.add(7);
list.add(1);

Any educated guess on what happens when you execute list.remove(1)? What about list.remove(new Integer(1))? This can cause some nasty bugs.

What is the proper way to differentiate between remove(int index), which removes an element from given index and remove(Object o), which removes an element by reference, when dealing with lists of integers?


The main point to consider here is the one @Nikita mentioned – exact parameter matching takes precedence over auto-boxing.

Java always calls the method that best suits your argument. Auto boxing and implicit upcasting is only performed if there’s no method which can be called without casting / auto boxing.

The List interface specifies two remove methods (please note the naming of the arguments):

  • remove(Object o)
  • remove(int index)

That means that list.remove(1) removes the object at position 1 and remove(new Integer(1)) removes the first occurrence of the specified element from this list.

You can use casting

list.remove((int) n);

and

list.remove((Integer) n);

It doesn’t matter if n is an int or Integer, the method will always call the one you expect.

Using (Integer) n or Integer.valueOf(n) is more efficient than new Integer(n) as the first two can use the Integer cache, whereas the later will always create an object.

I don’t know about ‘proper’ way, but the way you suggested works just fine:

list.remove(int_parameter);

removes element at given position and

list.remove(Integer_parameter);

removes given object from the list.

It’s because VM at first attempts to find method declared with exactly the same parameter type and only then tries autoboxing.

list.remove(4) is an exact match of list.remove(int index), so it will be called. If you want to call list.remove(Object) do the following: list.remove((Integer)4).

Any educated guess on what happens when you execute list.remove(1)? What about list.remove(new Integer(1))?

There is no need to guess. The first case will result in List.remove(int) being called, and the element at position 1 will be removed. The second case will result in List.remove(Integer) being called, and the element whose value is equal to Integer(1) will be removed. In both cases, the Java compiler selects the closest matching overload.

Yes, there is potential for confusion (and bugs) here, but it is a fairly uncommon use-case.

When the two List.remove methods were defined in Java 1.2, the overloads were not ambiguous. The problem only arose with the introduction of generics and autoboxing in Java 1.5. In hind-sight, it would have been better if one of the remove methods had been given a different name. But it is too late now.

Note that even if the VM did not do the right thing, which it does, you could still ensure proper behaviour by using the fact that remove(java.lang.Object) operates on arbitrary objects:

myList.remove(new Object() {
  @Override
  public boolean equals(Object other) {
    int k = ((Integer) other).intValue();
    return k == 1;
  }
}

Simply I did like following as suggested by #decitrig in accepted answer first comment.

list.remove(Integer.valueOf(intereger_parameter));

This helped me. Thanks again #decitrig for your comment. It may help for some one.

Simple Java Code Explanation for visualizing difference between ArrayList.remove(Object o) and ArrayList.remove(int index)

CODE :

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(7);
list.add(4);
list.add(5);
list.add(6);
list.add(7);

for (int a = 0; a < list.size(); a++) {
    System.out.print("" + list.get(a));
}

System.out.print("  ");

//CASE 1: We are removing data object 7.
list.remove(new Integer(7));

for (int a = 0; a < list.size(); a++) {
    System.out.print("" + list.get(a));
}

System.out.print("  ");

//CASE 2: Again we are removing data object 7.
list.remove(new Integer(7));

for (int a = 0; a < list.size(); a++) {
    System.out.print("" + list.get(a));
}

System.out.print("  ");

//CASE 3: We are removing data at index 1
list.remove(1);

for (int a = 0; a < list.size(); a++) {
    System.out.print("" + list.get(a));
}

OUTPUT :

1274567
124567
12456
1456

EXPLAINATION :

CASE 1: We are removing first occurring Integer object with data 7.

CASE 2: Then we are again doing same thing as in CASE 1 from remaining data.

CASE 3: We are removing data at index position 1.

CONCLUSION :

Taking an example of ArrayList<Integer> as used in above java code:
ArrayList.remove(Integer object) will simply remove the specific Integer object occurring first in the ArrayList.
However, ArrayList.remove(int index) will always remove the array element at given index position.

MORE ABOUT :

We cannot declare ArrayList<int> because it int is primary data-type. Any Class which has the base-class Object is only specified in the ArrayList generic datatype. eg: ArrayList<String>, ArrayList<Integer> etc.