Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - List isEmpty() method



Description

Groovy List isEmpty() method returns true if this List contains no elements.

Syntax

boolean isEmpty() 

Parameters

None

Return Value

True or false depending on whether the list is empty or not.

Example - Checking a empty List of integers

main.groovy

Following is an example of the usage of this method −

def lst = [11, 12, 13, 14]; 
def emptylst = [];

println(lst.isEmpty()); 
println(emptylst.isEmpty()); 

Output

When we run the above program, we will get the following result −

false 
true

Example - Checking a empty List of strings

main.groovy

Following is an example of the usage of this method −

def lst = ["Apple","Peach"]; 
def emptylst = [];

println(lst.isEmpty()); 
println(emptylst.isEmpty()); 

Output

When we run the above program, we will get the following result −

false 
true

Example - Checking a empty List of objects

Following is an example of the usage of this method −

main.groovy

def lst = [new Student(1, "Julie"),new Student(2, "Robert"),new Student(3, "Adam")];
def emptylst = [];

println(lst.isEmpty()); 
println(emptylst.isEmpty()); 

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

When we run the above program, we will get the following result −

false 
true
groovy_lists.htm
Advertisements