How to remove elements from ArrayList?

1. Introduction

This article is part of Complete Guide to ArrayList series. In previous articles of ArrayList, we saw how to create ArrayList and how to add elements in ArrayList. 

In this article we will see how to remove elements from ArrayList. 

2. Content

Below are three different APIs provided in List interface which is used remove elements from ArrayList.

boolean remove(Object o)

E remove(int index)

boolean removeAll(Collection<?> c)

3. boolean remove(Object o)

remove(Object o) method is used to remove the specified object in parameter to be removed from ArrayList. Now remember ArrayList allows duplicate elements so calling this method would remove the first occurrence only. If this object doesn’t exist in ArrayList, then ArrayList remains unchanged. This method returns true if it changes the ArrayList because of this call else returns false.

If it changed, then all elements appearing after Object o shifts to left by one place. Means the index of all those elements will decrements by 1.

remove() method does the comparison of the target element using equals method. So it is paramount that you have implemented equals and hashCode methods for the class whose elements are in ArrayList.

Example 1: Remove element from ArrayList.

List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Jack");

boolean isJaneRemoved = names.remove("Jane");
Assert.assertTrue(isJaneRemoved);
	
boolean isPikaRemoved = names.remove("Pikachu");
Assert.assertFalse(isPikaRemoved);

Example 2: Removing duplicate elements.

List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add(null);
names.add(null);

// remove nulls
names.remove(null);

Assert.assertEquals(3, names.size());

List contains : 
[John, Jane, null]

ArrayList allows duplicates and null elements.

4. E remove(int index)

remove(int index) method is an overloaded method. This method will just remove the element at the index specified in parameter.

If the index is < 0 or >= size of ArrayList, then this method will throw IndexOutOfBoundsException.

E remove(int index) method returns previous element at this index.

List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Jack");

String str = names.remove(2);
Assert.assertEquals("Jack", str);

5. boolean removeAll(Collection<?> c)

removeAll(Collection<?> c) method is a bulk modification operation where we remove all the elements specified in the Collection.

This method will throw a NullPointerException if the Collection<?> parameter is null.

If the values present in Collection<?> are present in ArrayList, then these elements will be removed. This method removes all the occurrences of the element specified in Collection from ArrayList. 

If it modifies the ArrayList because of this call then it returns true else returns false.

Example: name John is repeated 3 times. We will remove John and Jack as part of the removeAll method. Look closely, it will remove the name John along with all its repetitions from ArrayList.

List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("John");
names.add("Jack");
names.add("John");

// remove John and Jack
List<String> removeNames = new ArrayList<>();
removeNames.add("Jack");
removeNames.add("John");
names.removeAll(removeNames);

Assert.assertEquals(1, names.size());

Output: 
[Jane]

How to remove all null elements from ArrayList using removeAll?

List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add(null);
names.add(null);

// remove all nulls
names.removeAll(Collections.singletonList(null));

Assert.assertEquals(2, names.size());

6. Conclusion

In this article we saw how to remove elements from ArrayList using 3 different methods.

  1. remove(Object o): This method removes the specified object from ArrayList if that object exists in ArrayList.
  2. remove(int index): This method removes the object from the specified index if the index is in bounds, else throws IndexOutOfBoundsException.
  3. removeAll(Collection<?> c): This method removes all the elements specified in the Collection. Duplicate elements are removed from ArrayList if that element is specified in Collection.

In the next article we will discuss 4 different methods which check the existence of an element in ArrayList.

Leave a Reply

Your email address will not be published. Required fields are marked *