Remove Element(s) from ArrayList in Java

The Java ArrayList class is part of the Collection framework and allows to add and remove the elements using instance methods. Internally, it maintains a resizable array that grows or shrinks dynamically as a result of adding or removing the elements from it.

This tutorial discussed the different ways to remove single or multiple elements from an ArrayList using the remove(), removeAll() and removeIf() methods.

  • The remove() method removes a single element either by the specified value or from the specified index.
  • The removeAll() method removes all elements of the specified collection from the current list.
  • The removeIf() method removes all elements matching a Predicate.
ArrayList<String> arraylist2 = new ArrayList<>();

//1 - Remove an element from the specified index position
arraylist.remove(indexPosition);

//2 - Remove the first occurence of element by its value
arraylist.remove(element);

//3 - Remove all elements of the specified collection from arraylist
arraylist.removeAll(Arrays.asList(ele1, ele2, ele3));

//4 - Remove all elements matching a condition
arraylist.removeIf(e -> e.contains("temp"));

1. Syntax of remove(), removeAll() and removeIf() Methods

The remove() method is overloaded.

E remove(int index)
boolean remove(E element))
  • The remove(int index) – removes the element at the specified index and returns the removed item.
  • remove(E element) – remove the specified element by value and returns true if the element was removed, else returns false.

The removeAll() accepts a collection and removes all elements of the specified collection from the current list.

boolean removeAll(Collection<?> c)

The removeIf() accepts a Predicate to match the elements to remove.

boolean removeIf(Predicate<? super E> filter)

2. Remove an Element at Specified Index

The remove(index) method removes the specified element E at the specified position in this list. It removes the element currently at that position, and all subsequent elements are moved to the left (will subtract one from their indices).

Note that List Indices start with 0.

ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles") );
 
System.out.println(namesList);  //list size is 3
 
//Remove element at 1 index
namesList.remove(1);
 
System.out.println(namesList);  //list size is 2

Program output.

[alex, brian, charles]
[alex, charles]

2. Remove Element(s) By Value

The remove(Object) method removes the first occurrence of the specified element E in this list. As this method removes the object, the list size decreases by one.

ArrayList<String> namesList = new ArrayList<>(Arrays.asList( "alex", "brian", "charles", "alex") );
 
System.out.println(namesList);
 
namesList.remove("alex");
 
System.out.println(namesList);

Program output.

[alex, brian, charles, alex]
[brian, charles, alex]

To remove all occurrences of the specified element, we can use the removeAll() method. As this method accepts a collection argument, we first need to wrap the element to remove inside a List.

namesList.removeAll(List.of("alex"));

Program output.

[brian, charles]

3. Remove Element(s) with Matching Condition

We can use another super easy syntax from Java 8 stream to remove all elements for a given element value using the removeIf() method.

The following Java program uses List.removeIf() to remove multiple elements from the arraylist in java by element value.

ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles", "alex") );
 
System.out.println(namesList);
 
namesList.removeIf( name -> name.equals("alex"));
 
System.out.println(namesList);

Program output.

[alex, brian, charles, alex]
[brian, charles]

4. Conclusion

The ArrayList class (or any other list type in the collections framework) provides convenient methods to remove the elements using various techniques. Each technique serves a unique usecase.

Knowing when to use which remove method is key to use the ArrayList class effectively during data processing in the applications.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.