Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Collection findResult(Closure filteringTransform) method



Description

Groovy Map findResult(Closure predicate) method finds and returns matching non-null result while transforming items using the supplied closure, otherwise null is returned.

Syntax

public Object findResult(Closure predicate)

Parameters

predicate − 1 or 2 arguments closure used for matching

Return Value

list of non-null transformed values

Example - Finding matches 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"] 

// find matches starting from B using one argument closure
def result = map.findResults{ it.value.startsWith("B") ? "Found ${it.key}:${it.value}" : null }

println(result)

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

println(result)

Output

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

[Found B:Banana, Found D:Berry]
[Found B:Banana, Found D:Berry]

Example - Finding matches 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]

// find first even entry using one argument closure
def result = map.findResults{it.value % 2 == 0 ? "Found ${it.key}:${it.value}" : null}

println(result)

// find first even entry using two arguments closure
result = map.findResults{key, value -> value % 2 == 0 ? "Found ${key}:${value}":null}

println(result)

Output

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

[Found B:Banana, Found D:Berry]
[Found B:Banana, Found D:Berry]

Example - Finding matches 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"),4: new Student(4,"Mark")] 

// find first even entry using one argument closure
def result = map.findResults{it.key % 2 == 0 ? "Found ${it.key}:${it.value}": null}

println(result)

// find first even entry using two arguments closure
result = map.findResults{key, value -> key % 2 == 0 ? "Found ${key}:${value}":null}

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 −

[Found 2:[ 2, Robert ], Found 4:[ 4, Mark ]]
[Found 2:[ 2, Robert ], Found 4:[ 4, Mark ]]
groovy_maps.htm
Advertisements