Open In App

AbstractCollection size() Method in Java with Examples

Last Updated : 19 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

 



Previous Article
Next Article

Similar Reads

AbstractCollection clear() Method in Java with Examples
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: AbstractCol
2 min read
AbstractCollection addAll() Method in Java with Examples
The addAll(Collection C) method of Java AbstractCollection is used to append all of the elements from a mentioned collection to this collection. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the collection. Ret
2 min read
AbstractCollection add() Method in Java with Examples
The add() method in Java AbstractCollection is used to add a specific element into a Collection. This method will add the element only if the specified element is not present in the Collection else the function will return False if the element is already present in the Collection. Syntax: AbstractCollection.add(Object element) Parameters: The param
2 min read
AbstractCollection isEmpty() Method in Java with Examples
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 Fal
2 min read
AbstractCollection remove() Method in Java with Examples
The remove(Object O) method of Java AbstractCollection is to remove a particular element from a Collection. Syntax: AbstractCollection.remove(Object O) Parameters: The parameter O is of the type of Collection and specifies the element to be removed from the collection. Return Value: This method returns True if the element specified in the parameter
2 min read
AbstractCollection contains() Method in Java with Examples
The contains() method of Java AbstractCollection is used to check whether an element is present in a Collection or not. It takes the element as a parameter and returns True if the element is present in the collection.Syntax: AbstractCollection.contains(Object element) Parameters: The parameter element is of type Collection. This parameter refers to
2 min read
AbstractCollection containsAll() Method in Java with Examples
The containsAll() method of Java AbstractCollection is used to check whether two Collections contain the same elements or not. It takes one collection as a parameter and returns True if all of the elements of this collection is present in the other collection. Syntax: AbstractCollection.containsAll(Collection C) Parameters: The parameter C is a Col
2 min read
AbstractCollection toArray() Method in Java with Examples
1. toArray() The toArray() method of Java AbstractCollection is used to form an array of the same elements as that of the AbstractCollection. Basically, it copies all the element from a AbstractCollection to a new array. Syntax: Object[] arr = AbstractCollection.toArray() Parameters: The method does not take any parameters. Return Value: The method
5 min read
AbstractCollection toString() method in Java with Examples
The toString() method of JavaAbstractClass is used to return a string representation of the elements of the Collection. The String representation comprises a list representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets[].This method is used mainly to display collections other than Stri
2 min read
AbstractCollection in Java with Examples
The AbstractCollection class in Java is a part of the Java Collection Framework and implements the Collection interface. It is used to implement an unmodifiable collection, for which one needs to only extend this AbstractCollection Class and implement only the iterator and the size methods. Class Hierarchy: java.lang.Object ↳ java.util ↳ Class Abst
3 min read