In this section, we will learn what the List remove() and removeAll() methods are and how to use them in Java.
What is Java List remove() Method?
The Java List 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 List 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 List 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 List removeAll() Method?
The Java List 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 List 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 List 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