def list = [5, 10, 15, 20, 25, 30] def result = list.find { it > 10 } println resultThe 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.
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
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
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 fruitAs expected, the first item that has 6 characters and contains letter o is the output below:
Carrot
def map = [fruit: 'apple', color: 'blue', size:'large'] def item = map.find { key, value -> key.startsWith('c') } println item.key println item.valueIt 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
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