Java ArrayList removeall() Function

removeall Function

What is an ArrayList?

Here, in the page we will discuss about the ArrayList’s removeall() Method in java.
Arraylist is a part of collection framework in java. It contains some inbuilt function like removeAll() function which can be found in the java.util.ArrayList Package.It implements the List interface of the collections framework.

ArrayList removeAll Function:

Arraylist contains several inbuilt functions that are used to perform several operations on arraylist in Java. removeAll() function is also an inbuilt method of class ArrayList.
removeAll() method is used to remove or empty all the elements of an ArrayList and we can also remove some specified elements from an ArrayList using another object of some collection’s class.

Syntax:

arraylist_name.removeALL(collection c)

Definition of Parameters:

removeall Function

Return Type :

Exceptions:

clear() Function in ArrayList

clone() Function in ArrayList

add() Function in ArrayList

addall() Function in ArrayList

Example for Removing All Elements From ArrayList:

Just Like clear() method, We can also empty the whole ArrayList using the removeAll function in java for which the example code is given below.

Run
import java.util.*;

public class Main{
  public static void main(String[] args){
    // Initializing the new ArrayList
    ArrayList Numbers = new ArrayList<>();

    // Adding Elements to the ArrayList
    Numbers.add(1);
    Numbers.add(2);
    Numbers.add(3);

    // Printing elements of the ArrayList
    System.out.println("ArrayList: " + Numbers);

    // Invoking the removeAll Function
    Numbers.removeAll(Numbers);
    System.out.println("ArrayList: " + Numbers);
  }
}

Output:

ArrayList: [1, 2, 3]
ArrayList: []

Explanation:

In the above code, we have created an arraylist Numbers containing some integers and after invoking the removeAll() method the arraylist “Numbers” will remove all its elements and gets empty.

Example for Removing Elements of ArrayList in Comparison with Another ArrayList:

We can also clear some specified list of elements contained by another ArrayList with the help of removeAll() function for which the example code is given below:

Run
import java.util.*;

public class Main{
  public static void main(String[] args){
    // Initializing the new ArrayList
    ArrayList Numbers1 = new ArrayList<>();

    // Adding elements to the ArrayList
    Numbers1.add(1);
    Numbers1.add(2);
    Numbers1.add(3);

    // Printing elements of the ArrayList
    System.out.println("ArrayList 1: " + Numbers1);

    // Initializing the new ArrayList
    ArrayList Numbers2 = new ArrayList<>();

    // Adding Elements to the new ArrayList
    Numbers2.add(5);
    Numbers2.add(2);
    Numbers2.add(6);
    System.out.println("ArrayList 2: " + Numbers2);
    
    // Invoking removeAll Function
    Numbers1.removeAll(Numbers2);
    System.out.println("After Removing Elements ArrayList 1 will be: " + Numbers1);
  }
}

Output:

ArrayList 1: [1, 2, 3]
ArrayList 2: [5, 2, 6]
After Removing Elements ArrayList 1 will be: [1, 3]

Explanation:

In the above Example. we add 3 elements of integer type to the arraylist 1 and 3 elements to arraylist 2, after invoking the removeAll function on arraylilst 1, the common elements of arraylist 1 and arraylist 2 will get deleted in arraylist 1.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription