Collection clear() method in Java with Examples
The collection clear() method of Java Collection Interface clears the Collection upon which it is called. After this method is called, the collection will be empty as it removes all the elements from the collection.
This method does not take any parameter and does not return any value.
Example:
Java
import java.util.ArrayList;
import java.util.Arrays;
public class ClearMethodExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList( "1" , "2" , "3" , "4" ));
System.out.println( "List before clearing: " + list);
list.clear();
System.out.println( "List after clearing: " + list);
}
}
|
Output:
List before clearing: [1, 2, 3, 4]
List after clearing: []
Syntax
Collection.clear()
Parameters:
- This method does not accept any parameter
Returns:
- This method does not return any value.
Exceptions:
- UnsupportedOperationException: if the add operation is not supported by this collection
Collection clear() Method Examples
Let’s see some examples of how to use the collection clear() method in Java.
Example 1:
How to remove all elements from a collection using collection clear() Method
Java
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String args[]) {
Collection<String> list = new LinkedList<>();
list.add( "Geeks" );
list.add( "for" );
list.add( "Geeks" );
System.out.println( "The list is: " + list);
list.clear();
System.out.println( "The new List is: " + list);
}
}
|
Output
The list is: [Geeks, for, Geeks]
The new List is: []
Example 2:
Using the clear() method on ArrayDeque to remove its elements.
Java
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
Collection<String> de_que = new ArrayDeque<String>();
de_que.add( "Welcome" );
de_que.add( "To" );
de_que.add( "Geeks" );
de_que.add( "4" );
de_que.add( "Geeks" );
System.out.println( "ArrayDeque: " + de_que);
de_que.clear();
System.out.println( "The new ArrayDeque is: "
+ de_que);
}
}
|
Output
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The new ArrayDeque is: []
Example 3:
Using the clear() method on ArrayList
Java
import java.io.*;
import java.util.*;
public class ArrayListDemo {
public static void main(String[] args)
{
Collection<Integer> arrlist = new ArrayList<Integer>( 5 );
arrlist.add( 15 );
arrlist.add( 20 );
arrlist.add( 25 );
System.out.println( "ArrayList: " + arrlist);
arrlist.clear();
System.out.println( "The new ArrayList is: "
+ arrlist);
}
}
|
Output
ArrayList: [15, 20, 25]
The new ArrayList is: []
Reference: Official Oracle Documents
Whether you are a beginner starting Java programming or an experienced looking to brush up on your Java skills, this tutorial will provide you with a deep understanding of the collection clear function and its uses in Java.
The clear method in Java is a fundamental function for collection manipulation. With this guide, you can easily remove all elements of a collection using the clear function.
Read More Collection Methods
Last Updated :
16 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...