AbstractCollection isEmpty() Method in Java with Examples
Last Updated :
26 Nov, 2018
The isEmpty() method of Java AbstractCollection is used to check and verify if a Collection is empty or not. It returns True if the Collection is empty else it returns False.
Syntax:
AbstractCollection.isEmpty()
Parameters: The method does not take any parameter.
Return Value: The function returns True if the collection is empty else it returns False.
Below program illustrate the use of AbstractCollection.isEmpty() method:
Program 1:
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
public static void main(String args[])
{
AbstractCollection<String>
abs = new TreeSet<String>();
abs.add( "Welcome" );
abs.add( "To" );
abs.add( "Geeks" );
abs.add( "4" );
abs.add( "Geeks" );
abs.add( "TreeSet" );
System.out.println( "Collection: " + abs);
System.out.println( "Is the collection empty? "
+ abs.isEmpty());
abs.clear();
System.out.println( "Is the collection empty? "
+ abs.isEmpty());
}
}
|
Output:
Collection: [4, Geeks, To, TreeSet, Welcome]
Is the collection empty? false
Is the collection empty? true
Program 2:
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
public static void main(String args[])
{
AbstractCollection<String>
abs = new ArrayList<String>();
abs.add( "Welcome" );
abs.add( "To" );
abs.add( "Geeks" );
abs.add( "4" );
abs.add( "Geeks" );
abs.add( "ArrayList" );
System.out.println( "Collection: " + abs);
System.out.println( "Is the collection empty? "
+ abs.isEmpty());
abs.clear();
System.out.println( "Is the collection empty? "
+ abs.isEmpty());
}
}
|
Output:
Collection: [Welcome, To, Geeks, 4, Geeks, ArrayList]
Is the collection empty? false
Is the collection empty? true
Share your thoughts in the comments
Please Login to comment...