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:
import java.util.*;
public class GFG {
public static void main(String[] args)
{
List<Integer> arr = new ArrayList<Integer>( 10 );
boolean ans = arr.isEmpty();
if (ans == true )
System.out.println( "The List is empty" );
else
System.out.println( "The List is not empty" );
arr.add( 1 );
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()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Aug, 2020
Like Article
Save Article