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 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()); } } |
Collection: [4, Geeks, To, TreeSet, Welcome] The size of the Collection is: 5
Program 2:
// 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 Collectoin System.out.println("Collection:" + abs); // Displaying the size of the Collection System.out.println("The size of the Collection is: " + abs.size()); } } |
Collection:[Geeks, for, Geeks, 10, 20] The size of the Collection is: 5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- AbstractCollection add() Method in Java with Examples
- AbstractCollection contains() Method in Java with Examples
- AbstractCollection addAll() Method in Java with Examples
- AbstractCollection clear() Method in Java with Examples
- AbstractCollection toString() method in Java with Examples
- AbstractCollection remove() Method in Java with Examples
- AbstractCollection retainAll() method in Java with Examples
- AbstractCollection containsAll() Method in Java with Examples
- AbstractCollection isEmpty() Method in Java with Examples
- AbstractCollection toArray() Method in Java with Examples
- AbstractCollection in Java with Examples
- AbstractCollection removeAll() method in Java with Example
- ConcurrentSkipListMap size() method in Java with Examples
- SortedMap size() method in Java with Examples
- List size() method in Java with Examples
- ArrayList size() method in Java with Examples
- CharArrayWriter size() method in Java with examples
- CompoundName size() method in Java with Examples
- DelayQueue size() method in Java with Examples
- SortedSet size() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

