Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Map findResult(Object defaultResult, Closure predicate) method



Description

Groovy Map find(Object defaultResult, Closure predicate) method finds and returns first matching non-null result by passing each entry to the closure, otherwise default result is returned as defaultResult object.

Syntax

public Object findResult(Object defaultResult, Closure predicate)

Parameters

  • defaultResult − default result to be returned

  • predicate − 1 or 2 arguments closure used for matching.

Return Value

the first entry found otherwise defaultResult is returned

Example - Finding first match in a Map of String and String

Following is an example of the usage of this method −

main.groovy

// define a map
def map = ["A" : "Apple", "B" : "Banana", "C": "Carrot", "D":"Berry"] 

def defaultResult = "Not Found.";

// find first match starting from E using one argument closure
def result = map.findResult(defaultResult){ if(it.value.startsWith("E")) return "Found ${it.key}:${it.value}"}

println(result)

// find first match starting from B using two arguments closure
result = map.findResult(defaultResult){key, value -> if(value.startsWith("B")) return "Found ${key}:${value}"}

println(result)

Output

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

Not Found.
Found B:Banana

Example - Finding first match in a Map of Integer and Integer

Following is an example of the usage of this method −

main.groovy

// define maps
def map = [1 : 11, 2 : 12, 3: 13, 4:14]

def defaultResult = "Not Found.";

// find first even entry using one argument closure
def result = map.findResult(defaultResult){if(it.value > 15 ) return "Found ${it.key}:${it.value}"}

println(result)

// find first even entry using two arguments closure
result = map.findResult(defaultResult){key, value -> if(value < 15) return "Found ${key}:${value}"}

println(result)

Output

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

Not Found.
Found 1:11

Example - Finding First match in a Map of String and Object

Following is an example of the usage of this method −

main.groovy

// define maps
def map = [1 : new Student(1, "Julie"), 2 : new Student(2, "Robert"), 3: new Student(3,"Adam")] 

def defaultResult = "Not Found.";

// find first even entry using one argument closure
def result = map.findResult(defaultResult){if(it.key > 4) return "Found ${it.key}:${it.value}"}

println(result)

// find first even entry using two arguments closure
result = map.findResult(defaultResult){key, value -> if(key < 4) return "Found ${key}:${value}"}

println(result)

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 −

Not Found.
Found 1:[ 1, Julie ]
groovy_maps.htm
Advertisements