Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Groovy and Java 8

 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Outside of scripting benefits and possibly more concise code in Groovy, does the new Lambdas and Streams of Java 8 affect the relevancy of Groovy?
 
gunslinger & author
Posts: 151
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's great that Java 8 finally added lambdas, streams, and method references to the language. Being Java, they managed to do it in as verbose a fashion as possible, but so be it. The thing is, Groovy works fine with Java 8 right now.

For example, I can write:



Wherever Java expects a lambda, I can supply a Groovy closure. In Java 8, there is no class called "Lambda", so instead they use functional interfaces like Predicate and Consumer and assign lambdas to them. I just used Groovy closures instead, but the rest worked as expected.

What isn't supported (yet) is the new syntax for lambda or method references. Groovy uses closures for lambdas, of course, and it has its own syntax for method references (an & symbol). Still, all the new features in Java 8 are automatically available in Groovy as well.

Incidentally, the corresponding Groovy approach would be:

If you run that at the Groovy console you get [6, 18].

Even with the new Java 8 additions, it still doesn't have a meta-object protocol, which is necessary for metaprogramming. Still, everything else they added (static and default methods in interfaces, the new java.time package, and so on), you can use in Groovy without a problem.
 
Fredrik Liden
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, thanks for the overview.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic