Groovy Cookbook

Groovy programming language tutorials and howtos.

These 10 New Features Make Groovy 4.0 AWESOME!

Sealed types, switch expressions, and record types. Here are just a few new features introduced in the latest Groovy 4.0 release. In this video, I want to show you ten things that make Groovy 4.0 amazing. And to keep this video short, we’re not going to dive deep into each of them. Instead, I intend to give you a quick overview of the new features.

List of combinations from a list of lists in Groovy

Groovy has many useful functions built-in, and one of them is Iterable.combinations() that takes aggregated collections and finds all combinations of items. However, if we take a look its source code, we will find out that it was implemented using very imperative approach (nested for-loops + some if-statement). In this blog post I will show you how to implement the same function using Groovy and tail-recursion algorithm. Enjoy!

Tail-recursive methods in Groovy

Most of the object-oriented programmers prefer constructing algorithms using imperative style over using recursion. This is pretty obvious in the JVM ecosystem, where imperative iteration is much more efficient than recursive function call chain. However, what if I tell you that in Groovy you can take advantage of clean tail-recursive functions without sacrificing performance? Interested? Let’s deep dive into it.

How to avoid "No tests were found" when using JUnit 5 with Groovy?

In this short blog post I would like to explain how to avoid popular mistake when you write your first JUnit 5 test case in Groovy.

GraalVM and Groovy - how to start?

GraalVM became one of the most popular topics in the JVM ecosystem. It promises the highest possible speed of running JVM-based programs (when compiled to native images), hand in hand with the smaller memory footprint. It sounds interesting enough to give it a try. And today we are going to play around a little bit with running simple Groovy program after compiling to a standalone native image.

What is the most efficient way to iterate collection in Groovy? Let's play with JMH!

I guess you may heard about Groovy’s Collection.each(Closure cl) method - it was introduced 15 years ago [1] and it was a great alternative for a good old for-loop, for-each or even using an iterator approach. You may also heard, that you should not overuse it, because creating a closure to do such simple operation like collection iteration is an overhead. But what if I tell you that nothing could be further from the truth - Groovy’s each method may be faster than iterator or Java’s for-each. Sounds interesting? Enjoy the reading!

How Groovy's equal operator differs from Java?

One of the first mistakes people do when starting their journey with Java programming language is using == to compare objects instead calling a.equals(b). When you begin playing around with Groovy you quickly notice that equal operator == is used to compare objects in place of calling a.equals(b). "Finally something more intuitive!" you might think. In today’s article we will dig a little bit deeper to learn avoiding problems equal operator in Groovy may produce.

Groovy: static propertyMissing and methodMissing methods - limitations and possible issues

Some time ago I have found another interesting Groovy related question on Stack Overflow. This time someone was asking about static variants of popular propertyMissing and methodMissing methods. The official Groovy documentation does not explain how to do it - it only explains how to add any static method through metaClass. Today we are going to learn how to define these methods in two different ways.

Groovy: dynamic types coercion and promotion - you have been warned!

Groovy is a very powerful language on a JVM platform and with this great power comes great responsibility. There are many language features that are not intuitive for many people that start using Groovy. One of these features is dynamic coercion and type promotion which may cause you a headache if you use it carelessly.

How to name Groovy script file?

Some time ago I have found a very interesting question on Stack Overflow about forbidden characters in Groovy script filenames. Let’s use it as an excuse to make a closer look at Groovy script files naming limitations - you will find out that there are none actually.

Related YouTube videos