List removeall java – Java ArrayList removeAll() Method with Example

Java arraylist removeall: In the previous article we have discussed about Java ArrayList remove() Method with Example

In this article we are going to see the use Java ArrayList removeAll() method along with suitable examples.

Java ArrayList removeAll() Method with Example

removeAll( ):

List removeall java: This java.util.ArrayList.removeAll() method is used to remove all the elements from the arraylist or in the specified collection. It returns true if the original list is changed after calling removeAll() method.

Syntax:

arrayListName.removeAll(Collection col)

Where,

  • arrayListName refers to the name of your ArrayList.
  • Collection col refers to a collection ‘col’ whose all the elements will be removed.

Let’s see a program to understand it more clearly.

Approach:

  • Create a new ArrayList of type Integer.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Remove all the element from the ArrayList using removeAll( ) method.
  • Print the new arrayList

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of Integer datatype
        ArrayList<Integer> arr1 = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr1.add(1);
        arr1.add(2);
        arr1.add(3);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr1);
        // removing all the element from the ArrayList
        arr1.removeAll(arr1);
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList are: "+arr1);
    }
}

Output:

The elements of ArrayList  are: [1, 2, 3]
The new elements of ArrayList are: []

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: