AbstractCollection clear() Method in Java with Examples
Last Updated :
26 Nov, 2018
The clear() method of Java AbstractCollection in Java is used to remove all of the elements from the Collection. Using the clear() method only clears all the element from the collection and does not delete the collection. In other words, it can be said that the clear() method is used to only empty an existing AbstractCollection.
Syntax:
AbstractCollection.clear()
Return Value: The function does not return any value.
Below programs illustrate the AbstractCollection.clear() method:
Program 1:
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
public static void main(String[] args)
{
AbstractCollection<Object>
abs = new ArrayList<Object>();
abs.add( "Welcome" );
abs.add( "To" );
abs.add( "Geeks" );
abs.add( "4" );
abs.add( "Geeks" );
System.out.println( "AbstractCollection: "
+ abs);
abs.clear();
System.out.println( "AbstractCollection "
+ "after using clear: "
+ abs);
}
}
|
Output:
AbstractCollection: [Welcome, To, Geeks, 4, Geeks]
AbstractCollection after using clear: []
Program 2:
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
public static void main(String[] args)
{
AbstractCollection<Object>
abs = new LinkedList<Object>();
abs.add( 15 );
abs.add( 20 );
abs.add( 25 );
abs.add( 30 );
abs.add( 35 );
System.out.println( "AbstractCollection: "
+ abs);
abs.clear();
System.out.println( "AbstractCollection "
+ "after using clear: "
+ abs);
}
}
|
Output:
AbstractCollection: [15, 20, 25, 30, 35]
AbstractCollection after using clear: []
Share your thoughts in the comments
Please Login to comment...