Saturday, December 13, 2025
HomeLanguagesJavaList isEmpty() method in Java with Examples

List isEmpty() method in Java with Examples

The isEmpty() method of 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.

Syntax:

boolean isEmpty()

Parameter: It does not accepts any parameter.

Returns: 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 error or exceptions.

Program to demonstrate working of isEmpty() in Java:




// Java code to demonstrate the working of
// isEmpty() method in List interface
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // creating an Empty Integer List
        List<Integer> arr = new ArrayList<Integer>(10);
  
        // check if the list is empty or not
        // using isEmpty() function
        boolean ans = arr.isEmpty();
        if (ans == true)
            System.out.println("The List is empty");
        else
            System.out.println("The List is not empty");
  
        // addition of a element to
        // the List
        arr.add(1);
  
        // check if the list is empty or not
        // after adding an element
        ans = arr.isEmpty();
        if (ans == true)
            System.out.println("The List is empty");
        else
            System.out.println("The List is not empty");
    }
}


Output:

The List is empty
The List is not empty

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

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

2 COMMENTS

Most Popular

Dominic
32446 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12030 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7200 POSTS0 COMMENTS
Thapelo Manthata
6897 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS