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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  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.

Reinout Korbee user avatar by
Reinout Korbee
·
May. 15, 17 · Tutorial
Like (9)
Save
Tweet
Share
23.96K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Popular on DZone

  • Detecting Network Anomalies Using Apache Spark
  • Host Hack Attempt Detection Using ELK
  • Introduction Garbage Collection Java
  • Documentation 101: How to Properly Document Your Cloud Infrastructure Project

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: