Grails Cookbook - A collection of tutorials and examples

Groovy FindAll

Groovy is popular for writing short and concise codes. Programs that contains many lines of code using Java can be shortened in Groovy. This greatly helps in developer productivity. A feature that is commonly used in Groovy is in searching for items in a collection. The Groovy FindAll method searches for a subset of a collection that matches a given criteria. Here are some examples on how to use Groovy FindAll:

Groovy FindAll Syntax

The Groovy FindAll Syntax is short and simple. The closure that contains the criteria logic for filtering is passed to it. Here is a simple example that finds items in a list of Integer.
def list = [5, 10, 15, 20, 25, 30]
def result = list.findAll { it > 10 }
println result
The expression { it > 10 } is passed to findAll and this tells Groovy to find the subset where the items are greater than 10. The it variable represents the value of each item as they are iterated upon. Hence when it > 10 evaluates to true, then the item is added to the list that will be returned. For this case, multiple items are greater than 10. And since Groovy FindAll returns the subset, a list of values is returned. Here is the output of the code above:
[15, 20, 25, 30]

Note that findAll returns a collection. If you want to get only a single item (which is the first match on an item) , use Groovy Find instead.

No Match Result

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

As expected, the result is an empty list as shown in the output below:

[]

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.findAll { number -> number > 10 }
println result
def result2 = list.findAll { number -> number % 2 == 0 }
println result2

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

[15, 20, 25, 30]
[10, 20, 30]

FindAll From String List Examples

The expression for searching can contain methods from the items in the collection. Here are some examples on how to search from a list of String using Groovy FindAll. As shown below, we can use methods of a String in the condition expression.
def list = ['Apple', 'Banana', 'Carrot', 'Blueberry']
def fruits = list.findAll { item -> item.startsWith('B')}
println fruits

The above code finds list of items that starts with the letter B. As expected, here is the output.

[Banana, Blueberry]

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

def list = ['Apple', 'Banana', 'Carrot', 'Blueberry']
def fruits = list.findAll { item -> 
   item.length() >= 6 && item.contains('a')
}
println fruits
As expected, there are 2 items that have 6 or more characters and also contains the letter a. The output is below:
[Banana, Carrot]

FindAll From Map Examples

Since findAll 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 items = map.findAll { key, value -> key.size() >= 5 } 
println items
It searches for the subset where the key has at least 5 characters. Here is the output:
[fruit:apple, color:blue]

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

def map = [fruit: 'apple', color: 'blue', size:'large']      
def items = map.findAll { key, value -> value.contains('a') } 
println items

It searches for the items where the value contains the letter a:

[fruit:apple, size:large]

FindAll From List of POJO's

We can also use Groovy FindAll 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
   }
   public String toString() {
      return lastName + ", " + firstName
   }
}
And here is an example code that uses the POJO above. The Groovy FindAll is used to search for all persons with last name Doe.
class FindAllExample {
   static void main(String[] args) {
      def list = [
         new Person('John', 'Doe'),
         new Person('Jane', 'Smith'),
         new Person('James', 'Doe')
      ]
      def items = list.findAll { p -> p.lastName == 'Doe' }
      println items
   }
}

The result contains 2 persons. Here is the output:

[Doe, John, Doe, James]