Java ArrayList remove() removeAll() Methods Tutorial

In this section, we will learn what the List remove() and removeAll() methods are and how to use them in Java.

What is Java ArrayList remove() Method?

The Java ArrayList remove() method is used to remove an element from a list object.

For example, if you have a list of 10 elements and want to remove the element in the index number 2, you can use this method to run the operation.

Java ArrayList remove() Method Syntax:

E remove(int index)

remove() Method Parameters:

The method takes one argument, and that is the index number of an element that we want to remove from a list object.

remove() Method Return Value:

The return value of this method is the removed element from the target list.

remove() Method Exceptions:

This method might throw 3 types of exceptions:

ClassCastException: You get this exception if you set an argument of type other than integer.

NullPointerException: If you put the value null as the argument of this method, you’ll get this exception.

UnsupportedOperationException: If this method is not supported in the target list object and we try to use it anyway, we’ll get this exception.

Example: using ArrayList remove() method

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

public class Main {

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Six");
        list.add("Four");
        list.add("Five");
        list.add("Six");

        System.out.println("The size of the list before removing an element from it: "+ list.size());
        String removed = list.remove(5);
        System.out.println("The removed element is: "+ removed);
        System.out.println("The size of the list after removing an element from it: "+ list.size());
    }
}

Output:

The size of the list before removing an element from it: 7

The removed element is: Five

The size of the list after removing an element from it: 6

What is Java ArrayList removeAll() Method?

The Java ArrayList removeAll() method is used to remove more than one element from a list object.

Basically, this method takes a list object as its argument and for every element in this list, it checks the main list object (the one that the method is called with) to see if such an element is there or not. If it has, that element will be deleted then. And this process happens for every element in the argument list object.

Java ArrayList removeAll() Method Syntax:

boolean removeAll(Collection<?> c)

removeAll() Method Parameters:

The method takes one argument and that is the list object we want to search its elements in the target list (the main list that this method is called with) and remove any match element in it.

removeAll() Method Return Value:

The return value of this method is a boolean indicating if the operation was successful or not.

If the value of this method is true, it means the operation was a success. Otherwise you’ll get the value false.

removeAll() Method Exceptions:

This method throws the same sort of exceptions that the remove() method might throw.

Example: using ArrayList removeAll() method

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

public class Main {

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Six");
        list.add("Four");
        list.add("Five");
        list.add("Six");

        List<String> list2 = new ArrayList<>();
        list2.add("Ten");
        list2.add("Five");

        System.out.println("The size of the list before removing an element from it: "+ list.size());
        boolean success = list.removeAll(list2);
        if (success){
         System.out.println("The operation was successful");
        }else{
         System.out.println("Failed operation");
        }
        System.out.println("The size of the list after removing an element from it: "+ list.size());
    }
}

Output:

The size of the list before removing an element from it: 7

The operation was successful

The size of the list after removing an element from it: 6

What is Java ArrayList removeIf() Method?

The Java ArrayList removeIf() method is used to remove the elements of a list based on a condition.

Basically, this method runs a condition on each element of a list object. Any of these elements that cause the condition to result true will be deleted from the list.

For example, let’s say you have a list of numbers and want to remove those elements that are larger than 50! Well, using this method, you can remove those elements that are higher than this value.

Java ArrayList removeIf() Method Syntax:

default boolean removeIf(Predicate<? super E> filter)

removeIf() Method Parameters:

The method takes one argument and that is a reference to a method.

This reference method is the place where we define the condition to be executed on every element of a list object.

We could create this reference method using lambda expression or using instance or static methods if there’s one already exists.

Here’s the signature of the reference method:

  • It should take one argument that is of the same type as the elements of the target list object.
  • The return value of that method should be of Boolean type.

Note: As mentioned before, this reference method will be executed for each and every element of a list object. If the result of executing this method is true, then the related element will be removed from the list, otherwise if the result is false, the element will stay in that list.

removeIf() Method Return Value:

The return value of the removeIf() method is a Boolean value.

This value is true if one or more elements of the target list object was removed. And false otherwise.

removeIf() Method Exceptions:

NullPointerException: this method will be thrown if we put a null value as the argument of the removeIf() method.

UnsupportedOperationException: You’ll get this exception of this method is not supported in the target list object.

Example: using ArrayList removeIf() method

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

public class Main {

    public static void main(String[] args){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);

       list.removeIf(e->{
         return e>5;
       });

       list.forEach(e->{
         System.out.println(e);
       });
    }
}

Output:

1

2

3

4

5

In this example, the condition of the reference method that we’ve set as the argument of the removeIf() is to check and see if an element of the list object is higher than the value 5.

Now if any element in this list is higher than the value 5, the result of the reference method becomes true and hence that element will be deleted from the list.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies