Master the Groovy grep function with this tutorial. Learn how to filter and search lists with practical examples.
last modified February 25, 2025
The grep function in Groovy is a versatile tool for filtering and searching elements in a list. It allows you to select elements that match a specific condition, such as a pattern, type, or custom logic. This tutorial covers the grep function with 15 practical examples.
The grep function filters a list based on a condition. It returns a new list containing only the elements that match the condition.
BasicGrep.groovy
def numbers = [1, 2, 3, 4, 5, 6] def evenNumbers = numbers.grep { it % 2 == 0 }
println(evenNumbers)
This example filters even numbers from a list using the grep function.
You can use grep to filter elements of a specific type.
FilterByType.groovy
def mixedList = [1, “Groovy”, 3.14, true, “Java”] def strings = mixedList.grep(String)
println(strings)
This example filters only string elements from a mixed list.
grep can filter elements that match a regular expression.
FilterByRegex.groovy
def words = [“apple”, “banana”, “cherry”, “date”] def aWords = words.grep(~ /a/)
println(aWords)
This example filters words containing the letter “a”.
You can use a range to filter elements within a specific range.
FilterByRange.groovy
def numbers = [10, 20, 30, 40, 50] def rangeFiltered = numbers.grep(20..40)
println(rangeFiltered)
This example filters numbers within the range 20 to 40.
You can use a closure to define a custom filtering condition.
CustomCondition.groovy
def numbers = [1, 2, 3, 4, 5, 6] def greaterThanThree = numbers.grep { it > 3 }
println(greaterThanThree)
This example filters numbers greater than 3.
You can use grep to filter out null values from a list.
FilterNullValues.groovy
def mixedList = [1, null, “Groovy”, null, 3.14] def nonNullValues = mixedList.grep { it != null }
println(nonNullValues)
This example removes null values from a list.
You can filter elements that are instances of a specific class.
FilterByClass.groovy
def mixedList = [1, “Groovy”, 3.14, true] def integers = mixedList.grep(Integer)
println(integers)
This example filters only integer values from a mixed list.
You can combine multiple conditions in a closure for filtering.
MultipleConditions.groovy
def numbers = [1, 2, 3, 4, 5, 6] def filtered = numbers.grep { it % 2 == 0 && it > 3 }
println(filtered)
This example filters even numbers greater than 3.
You can filter elements that are contained in another collection.
FilterByCollection.groovy
def numbers = [1, 2, 3, 4, 5, 6] def filterList = [2, 4, 6] def filtered = numbers.grep(filterList)
println(filtered)
This example filters elements that are present in another list.
You can use the index of elements in the filtering condition.
FilterWithIndex.groovy
def numbers = [10, 20, 30, 40, 50] def filtered = numbers.grep { it, index -> index % 2 == 0 }
println(filtered)
This example filters elements at even indices.
You can filter objects based on their properties.
FilterByProperties.groovy
class Person { String name int age }
def people = [ new Person(name: “Alice”, age: 30), new Person(name: “Bob”, age: 25), new Person(name: “Charlie”, age: 35) ]
def adults = people.grep { it.age >= 30 }
println(adults.name)
This example filters people aged 30 or older.
You can filter strings using case-insensitive matching.
CaseInsensitiveMatch.groovy
def words = [“Apple”, “banana”, “Cherry”, “Date”] def aWords = words.grep(~ /(?i)a/)
println(aWords)
This example filters words containing “a” or “A”.
You can filter elements that do not match a condition.
NegativeCondition.groovy
def numbers = [1, 2, 3, 4, 5, 6] def notEven = numbers.grep { !(it % 2 == 0) }
println(notEven)
This example filters numbers that are not even.
You can filter elements based on custom logic in an object.
CustomObjectFilter.groovy
class Filter { boolean isEven(n) { n % 2 == 0 } }
def numbers = [1, 2, 3, 4, 5, 6] def filter = new Filter() def evenNumbers = numbers.grep(filter.&isEven)
println(evenNumbers)
This example uses a custom object to filter even numbers.
You can filter elements that match any of multiple patterns.
MultiplePatterns.groovy
def words = [“apple”, “banana”, “cherry”, “date”] def patterns = [~ /a/, ~ /e/] def filtered = words.grep { word -> patterns.any { word =~ it } }
println(filtered)
This example filters words that contain either “a” or “e”.
Use Descriptive Conditions: Write clear and concise conditions for better readability. Combine Conditions: Use logical operators to combine multiple conditions. Optimize Performance: Avoid complex conditions for large lists to improve performance. Leverage Regular Expressions: Use regex for powerful pattern matching.
In this tutorial, we explored the grep function in Groovy with 15 practical examples. The grep function is a powerful tool for filtering and searching lists, making it easier to work with collections in Groovy.
My name is Jan Bodnar, and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.
List all Groovy tutorials.