The Wayback Machine - https://web.archive.org/web/20250112100812/https://www.geeksforgeeks.org/list-isempty-method-in-java-with-examples/
Open In App

List isEmpty() method in Java with Examples

Last Updated : 03 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The isEmpty() method of the List interface in Java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element.

Example:

Java
// Java Progrm to Implement
// List isEmpty() Method
import java.util.*;

class Main 
{
    public static void main (String[] args) 
    {
          // New empty list created
        List<Integer> l=new ArrayList<>();
          
          // Implementing isEmpty() Method
          if(l.isEmpty())
              System.out.println("List is Empty");
          else
              System.out.println("List is not Empty");
    }
}

Output
List is Empty


Syntax of the Method

boolean isEmpty()

  • Parameter : It does not accept any parameter.
  • Return Type : It returns True if the list has no elements else it returns false. The return type is of datatype boolean.
  • Error and Exceptions : This method has no errors or exceptions.

Program Demonstrating List isEmpty() in Java

Below is the implementation of isEmpty() Method:

Java
// Java Program Implementing 
// List isEmpty() Method
import java.util.List;
import java.util.ArrayList;

public class Main 
{
    public static void main(String[] args)
    {
      
          // Creating Empty ArrayList
        List<Integer> arr = new ArrayList<Integer>();
      
          // Demonstrating isEmpty() Method
          System.out.print(arr + " : ");
        System.out.println(arr.isEmpty());
        
        arr.add(1);
          arr.add(2);
          arr.add(3);
      
          // Demonstrating isEmpty() Method
        System.out.print(arr + " : ");
          System.out.println(arr.isEmpty());
    }
}

Output
[] : true
[1, 2, 3] : false

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/List.html#isEmpty()

Start your Java programming journey today with our Java Programming Online Course, designed for both beginners and advanced learners. With self-paced lessons covering everything from basic syntax to advanced concepts, you’ll gain the skills needed to excel in the world of programming.

Take the Three 90 Challenge! Complete 90% of the course in 90 days, and earn a 90% refund. Track your progress and stay motivated as you master Java.
Join now and start your path to becoming a Java expert!


Next Article

Similar Reads

three90RightbarBannerImg