Thursday, November 20, 2025
HomeLanguagesJavaAbstractCollection size() Method in Java with Examples

AbstractCollection size() Method in Java with Examples

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




// Java code to illustrate size()
 
import java.util.*;
import java.util.AbstractCollection;
 
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
 
        // Creating an empty Collection
        AbstractCollection<String>
            abs = new TreeSet<String>();
 
        // Use add() method to add
        // elements into the Collection
        abs.add("Welcome");
        abs.add("To");
        abs.add("Geeks");
        abs.add("4");
        abs.add("Geeks");
        abs.add("TreeSet");
 
        // Displaying the Collection
        System.out.println("Collection: " + abs);
 
        // Displaying the size of the collection
        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




// Java code to illustrate size()
 
import java.util.*;
import java.util.AbstractCollection;
 
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
 
        // Creating an empty Collection
        AbstractCollection<String>
            abs = new LinkedList<String>();
 
        // Using add() method to add
        // elements in the Collection
        abs.add("Geeks");
        abs.add("for");
        abs.add("Geeks");
        abs.add("10");
        abs.add("20");
 
        // Displaying the Collection
        System.out.println("Collection:" + abs);
 
        // Displaying the size of the Collection
        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

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6904 POSTS0 COMMENTS
Ted Musemwa
7159 POSTS0 COMMENTS
Thapelo Manthata
6859 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS