Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Get the Size of Collection and Verify that Collection is Empty

  • Difficulty Level : Medium
  • Last Updated : 07 Jan, 2021

The size() and isEmpty() of java.util.Collection interface is used to check the size of collections and if the Collection is empty or not. isEmpty() method does not take any parameter and does not return any value. 

Example:

Input:[99, 54, 112, 184, 2]
Output:size = 5 and Collection is not empty

Input:[]
Output: size = 0 and Collection is empty

size() method:

Syntax: 

public int size()

Parameters: This method does not takes any parameters.

Return Value: This method returns the number of elements in this list.



isEmpty() method:

Syntax:

Collection.isEmpty()

Parameters: This method do not accept any parameter

Return Value: This method does not return any value.

Example 1: Using LinkedList Class

Java




//Java Program to Get the Size of Collection 
// and Verify that Collection is Empty
// using LinkedList class
  
import java.io.*;
import java.util.*; 
  
class GFG {
    public static void main (String[] args) {
        
        Collection<Integer> list = new  LinkedList<Integer>();  
        
        //add integer values in this list  
        list.add(99);  
        list.add(54);  
        list.add(112);
          list.add(184);
          list.add(2);
        
        // Output the present list 
        System.out.print("The elements in Collection : ");  
        System.out.println(list);  
        
        //returns the size of the list
        System.out.println("Size of the collection "+list.size()); 
        
        // Check if list is empty using isEmpty() method 
        System.out.println("Is the LinkedList empty: "
                           + list.isEmpty()); 
        // Clearing the LinkedList 
        list.clear(); 
    
        // printing the new list 
        System.out.println("The new List is: " + list); 
    
        // Check if list is empty 
        // using isEmpty() method 
        System.out.println("Is the LinkedList empty: "
                           + list.isEmpty()); 
    }
}
Output
The elements in Collection : [99, 54, 112, 184, 2]
Size of the collection 5
Is the LinkedList empty: false
The new List is: []
Is the LinkedList empty: true

Example 2:  Using ArrayList Class

Java




//Java Program to Get the Size of Collection and 
// Verify that Collection is Empty 
// using ArrayList class
  
import java.io.*;
import java.util.*; 
  
class GFG {
    public static void main (String[] args) {
        
        Collection<String> arraylist = new ArrayList<String>();  
        
           arraylist.add("Geeks");  
        arraylist.add("for");  
        arraylist.add("geeks");
        
        // returns the size of the arraylist
        System.out.println("Size of the collection "+arraylist.size()); 
        
        // Check if list is empty using isEmpty() method 
        System.out.println("Is the ArrayList empty: "
                           + arraylist.isEmpty()); 
    }
}
Output
Size of the collection 3
Is the ArrayList empty: false

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :