Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - List withEagerDefault(Closure init) method



Description

Groovy List withEagerDefault(Closure init) method decorates the list allowing it to grow when a non-existing index is referred outside the normal list bounds. When called with such indexes, list is grown in size and a default value is assigned in place by calling the provided init closure and to every uninitialized values.

Syntax

public ListwithDefault withEagerDefault(Closure init)

Parameters

init − a closure with target index as parameter.

Return Value

The decorated list.

Example - Getting Default value from a List of Integers

Following is an example of the usage of this method −

main.groovy

// pass a default value as 0
def lst = [11, 12].withEagerDefault {0}

// access list elements
println(lst[3])  // default value 0 will be assigned
println(lst)

// pass a default value as computed value
lst = [11, 12].withEagerDefault {index -> index * index}
println(lst[4])  // default value 16 will be assigned
println(lst)

// try to access null Value
lst[3] = null
println(lst[3]) // Now null can be accessed

Output

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

0
[11, 12, 0, 0]
16
[11, 12, 4, 9, 16]
null

Example - Getting Default value from a List of Strings

Following is an example of the usage of this method −

main.groovy

// pass a default value as Empty
def lst = ["Apple", "Banana"].withEagerDefault {"Empty"}

// access list elements
println(lst[3])  // default value "Empty" will be assigned
println(lst)

// pass a default value as computed value
lst = ["Apple", "Banana"].withEagerDefault {index -> "Empty #" + index}
println(lst[4])  // default value will be assigned
println(lst)

// try to access null Value
lst[3] = null
println(lst[3]) // Now null is accessible

Output

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

Empty
[Apple, Banana, Empty, Empty]
Empty #4
[Apple, Banana, Empty #2, Empty #3, Empty #4]
null

Example - Getting Unique elements from a List of Objects

Following is an example of the usage of this method −

main.groovy

def defaultStudent = new Student(-1, "System")

def lst = [new Student(1, "Julie"),new Student(2, "Robert")].withEagerDefault{defaultStudent}

// access list elements
println(lst[3])  // default value new Student(-1, "System") will be assigned
println(lst)

class Student implements Comparable {
   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 + " ]";
   }
   
   public int compareTo(Object obj){
      Student s = (Student)obj;
      return this.rollNo - s.rollNo   
   }
}

Output

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

[ -1, System ]
[[ 1, Julie ], [ 2, Robert ], [ -1, System ], [ -1, System ]]
groovy_lists.htm
Advertisements