Grails Cookbook - A collection of tutorials and examples

Groovy Find

Groovy is one of the coolest language available because it makes coding so much easier. Long Java codes can be written in Groovy in fewer lines that are much more readable. One of the most used feature in Groovy is in searching. The Groovy Find method can be used to search for a single object in a collection, given a condition on what needs to be searched for. Below are some examples on how to use Groovy Find for searching.

Groovy Find Syntax

The Groovy Find Syntax is simple. The closure that contains the logic for matching items in a collection is passed to it. Here is a simple example that finds an item in a list of Integer.
def list = [5, 10, 15, 20, 25, 30]
def result = list.find { it > 10 }
println result
The expression { it > 10 } is the closure that contains the matching logic. The it variable represents the value of each item as they are iterated upon. Hence when it > 10 evaluates to true, then the item on that iteration is returned as result. For this case, the first item that is greater than 10 is 15. And since Groovy Find returns only one item, the value 15 is returned. Here is the output of the code above:
15

Note that find returns only a single item in a collection. If you want to get the list that matches a criteria, use Groovy FindAll.

No Match Result

When no items in the list match the criteria, the null value is returned by Groovy find. Here is an example code:
def list = [5, 10, 15, 20, 25, 30]
def result = list.find { it > 100 }
println result

As expected, the result is null as shown in the output below:

null

Variable Name

For readability, you may give a name to replace the it variable. Here is an example:

def list = [5, 10, 15, 20, 25, 30]
def result = list.find { number -> number > 10 }
println result
def result2 = list.find { number -> number % 2 == 0 }
println result2

The variable name number is used instead of it. The first find statement looks for the first number in the list greater than 10. The second find statement looks for the first number that is even. Here is the output.

15
10

Find From String List Examples

We can use any methods of an item in the list for our expression. Here are some examples on how to search from a list of String using Groovy Find. As shown below, we can use methods of a String in the condition expression.
def list = ['Apple', 'Banana', 'Carrot']
def fruit = list.find { item -> item.startsWith('B')}
println fruit

The above code finds the first item that starts with the letter B. As expected, here is the output.

Banana

Our expression can be more complex. Here is an example that uses logical operator &&:

def list = ['Apple', 'Banana', 'Carrot']
def fruit = list.find { item ->
   item.length() == 6 && item.contains('o')
}
println fruit
As expected, the first item that has 6 characters and contains letter o is the output below:
Carrot

Find From Map Examples

Since find can be used for collections, it follows that it is applicable with map. Here is a simple example:
def map = [fruit: 'apple', color: 'blue', size:'large']      
def item = map.find { key, value -> key.startsWith('c') } 
println item.key
println item.value
It searches for the first item where the key starts with letter c. The result is the second item in the list. Here is the output:
color
blue

Here is another example where the value is used in the criteria:

def map = [fruit: 'apple', color: 'blue', size:'large']      
def item = map.find { key, value -> value.length() >= 5 } 
println item.key
println item.value

It searches for the first item where the value has 5 or more characters:

fruit
apple

Find From List of POJO's

We can also use Groovy Find to search from a list of POJOs. Here is an example POJO:

class Person {
   String firstName, lastName
   public Person (String firstName, String lastName) {
      this.firstName = firstName
      this.lastName = lastName
   }
}
And here is an example code that uses the POJO above. The Groovy Find is used to search for the first Person with the last name Roberts.
class FindExample {
   static void main(String[] args) {
      def list = [
         new Person('John', 'Doe'),
         new Person('Jane', 'Smith'),
         new Person('James', 'Roberts')
      ]
      def item = list.find { p -> p.lastName == 'Roberts' }
      println item.lastName + "," + item.firstName
   }
}

The output is the third item on the list:

Roberts,James