DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Trending

  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI
  • How to Format Articles for DZone
  • Fix the Target, Precompute Once: A Backend-Free Word-Ladder Solver With a BFS Distance Field
  • How Kestra Unifies Enterprise Workflow Orchestration and Agentic AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Mixing Java Functions and Groovy Closures

Mixing Java Functions and Groovy Closures

When mixing Java and Groovy, the time might come when you want to dip into Java's functional features, but while using Groovy's syntax. Enter closures.

By 
Reinout Korbee
·
May. 15, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
26.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

With Java 8 offering functional features, the question comes to mind, "How and why Java functions can be used in Groovy?"

In many ways, closures and functions more or less offer the same functionality. Groovy has support for functions, but not for lambdas. The syntax conflicts because the arrow symbol is already used in Groovy. In what way can the two, Java functions and groovy closures, be used together?

The first example, a Java functional interface is implemented with a Closure in Groovy:

List list = [1,4,2,3,7,4]
BiFunction greaterThen_java = { 
    Integer a, Integer b ->
    a > b ? true : false
}
assert greaterThen_java.apply(1, 2) == false


This is really the same as the following in Groovy:

def greaterThen_groovy = { Integer a, Integer b ->
    a > b ? true : false
}
assert greaterThen_groovy.call(1 ,2) == false


Except for the typing information, which is ignored anyway, the two examples are semantically and syntactically almost identical.

Some Java methods take functions as arguments. For example, Collection’s removeIf method:

Predicate greaterThen2 = { a ->
    a > 2 ? true : false
}
list.removeIf(greaterThen2)
assert list.size() == 2 && list[0] == 1 && list[1] == 2


In the above example, the predicate is implemented with a closure and passed to the Java method removeIf, which executes the predicate. Groovy and Java syntax and functionality nicely mix together.

Unfortunately, the lambda syntax isn’t supported in Groovy. A closure will do, however. Take the following example:

def min = { Integer a, Integer b -> 
    a < b ? a : b 
} 
Integer result = list.stream().reduce( min ).get() 
assert result == 1


Instead of using Java syntax for the reduce method, we pass a closure with the correct input parameters. The Java streams functionality can then be used as usual. If you keep in mind when to use functions and closures, both Java and Groovy's functional features can be used together, creating an even more powerful combination.

Java (programming language) Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook