AbstractCollection size() Method in Java with Examples
Last Updated :
19 May, 2021
The size() method of Java AbstractCollection is used to get the size of the Collection or the number of elements present in the Collection.
Syntax:
AbstractCollection.size()
Parameters: The method does not take any parameter.
Return Value: The method returns the size or the number of elements present in the collection.
Below programs illustrates the use of AbstractCollection.size() method:
Program 1:
Java
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( "The size of the Collection is: "
+ abs.size());
}
}
|
Output:
Collection: [4, Geeks, To, TreeSet, Welcome]
The size of the Collection is: 5
Program 2:
Java
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
public static void main(String args[])
{
AbstractCollection<String>
abs = new LinkedList<String>();
abs.add( "Geeks" );
abs.add( "for" );
abs.add( "Geeks" );
abs.add( "10" );
abs.add( "20" );
System.out.println( "Collection:" + abs);
System.out.println( "The size of the Collection is: "
+ abs.size());
}
}
|
Output:
Collection:[Geeks, for, Geeks, 10, 20]
The size of the Collection is: 5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...