Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - List withDefault(Closure init) method



Description

Groovy List withDefault(Closure init) method is an alias for withLazyDefault() 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. As a result of this operation, a null cannot be stored in the list as it always returns the computed default value.

Syntax

public ListWithDefault withDefault(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].withDefault {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].withDefault {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]) // default value 9 will be printed

Output

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

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

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"].withDefault {"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"].withDefault {index -> "Empty #" + index}
println(lst[4])  // default value will be assigned
println(lst)

// try to access null Value
lst[3] = null
println(lst[3]) // default value will be printed

Output

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

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

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")].withDefault{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 ], null, [ -1, System ]]
groovy_lists.htm
Advertisements