Understanding ArrayList remove() methods in Java

In this article, we will focus on the remove() methods provided by ArrayList to remove elements.

 
arraylist remove
 
ArrayList provides two overloaded remove() methods .. they are :

 

  1. remove(int index)

     

    This method removes the element at the specified position in this list and then shifts any subsequent elements to the left.

  2.  

  3. remove(Object obj)

     

    This method removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged.

 

 

These methods may look straight forward, but here are some scenarios when things get confusing:

 

Scenario 1

 

What happens when we invoke remove(10) on an ArrayList. Is 10 an index here or does it refer to an element ?

 

package com.topjavatutorial;

import java.util.ArrayList;
import java.util.List;

public class ArrayListDemo2 {

  public static void main(String[] args) {

    List list = new ArrayList();
    
    list.add(10);
    list.add(10);
    
    System.out.print(list.size());
    
    list.remove(10);
    
    System.out.print(list.size());
  }

}

Here remove(10) will try to remove the element at array position 10. It won’t autobox the int value to Integer to use the remove(Object) method.

 

Since the above example has only 2 elements in the arraylist, remove(10) will throw java.lang.IndexOutOfBoundsException

 

Scenario 2

 

What if we have an element duplicated in the ArrayList ? Will remove() method remove one occurrence or all the matching elements ?

 

To remove an element with this method, ArrayList searches through the elements to find a match.

 

If a match is found, then remove(Object) removes the first occurrence of the element found.

 

package com.topjavatutorial;

import java.util.ArrayList;
import java.util.List;

public class ArrayListDemo {

  public static void main(String[] args) {

    List list = new ArrayList();
    
    list.add(10);
    list.add(10);
    
    System.out.print(list.size());
    
    list.remove(new Integer(10));
    
    System.out.print(list.size());
  }

}


 

Scenario 3

 

How does ArrayList compare elements when remove is called ?

 

To remove an element, an ArrayList first searches through its elements to find an element that can be considered equal to the target element.

The equality check is done by calling equals() method on the target object and its own objects.

 

In below example, we are creating a list of strings and then using remove method to remove a string.

 

package com.topjavatutorial;

import java.util.ArrayList;
import java.util.List;

public class ArrayListStringDemo {

  public static void main(String[] args) {

    List countries = new ArrayList();
    
    countries.add("Australia");
    countries.add("Canada");
    countries.add("India");
    countries.add("USA");
    
    System.out.println(countries);
    //[Australia, Canada, India, USA]
    
    countries.remove("USA");
    
    System.out.println(countries);
    //[Australia, Canada, India]
  }

}


Since String class already overrides equals() method to check for String value, it works as expected.

 

Running the above program will print :

 

[Australia, Canada, India, USA]

[Australia, Canada, India]

 

 

However, remove() won’t work, if we are using it on an ArrayList of objects of a Class that does not override the equals method.

 

This is because the equals() method will use the default implementation which will compare the references and not value.

 

Here is an example demonstrating this :

package com.topjavatutorial;

public class Employee {

  private int id;
  
  private String name;
  
  Employee(int id, String name){
    this.id = id;
    this.name = name;
  }
  
}

package com.topjavatutorial;

import java.util.ArrayList;
import java.util.List;

public class ArrayListRemoveWithoutEquals {

public static void main(String[] args) {

List empList = new ArrayList();

empList.add(new Employee(1,"A"));
empList.add(new Employee(1,"B"));
empList.add(new Employee(1,"C"));

System.out.println(empList.size());

//prints 3

//remove Employee Neo

empList.remove(new Employee(1,"A"));

System.out.println(empList.size());

//prints 3
}

}

The above example won’t remove the employee A and would still print size of list as 3.

To make it work as expected, we should override equals() method in the Employee class to compare the id and name for comparison.

 
 

You may also like following articles on Java Collections:

 

 
 

© 2016 – 2017, https:. All rights reserved. On republishing this post, you must provide link to original post

3 comments

  1. Thank you very much

  2. […] Understanding ArrayList remove() method   […]

  3. Abhishek Bansal

    good one..

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: