Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - List getAt(EmptyRange range) method



Description

Groovy List getAt(EmptyRange range) method supports the range subscript operator for the list.

Syntax

public List getAt(EmptyRange range)

Parameters

range − a Range indicating the items to get.

Return Value

a new list instance based on range borders

Example - Applying Range operator on List of Integers

Following is an example of the usage of this method −

main.groovy

def lst = [1,2, 3, 4, 5]
def rangeLst = lst.getAt(0..<0)

println(rangeLst)

Output

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

[]

Example - Applying Range operator on List of Strings

Following is an example of the usage of this method −

main.groovy

def lst = ["Apple","Mango","Orange", "Papaya", "Peach"]
def rangeLst = lst.getAt(0..<0)

println(rangeLst)

Output

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

[]

Example - Applying Range operator on 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 rangeLst = lst.getAt(0..<0)

println(rangeLst)

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
   
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

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

[]
groovy_lists.htm
Advertisements