100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Groovy Cheat Sheet

Groovy Cheat Sheet

Practical Groovy syntax covering closures, collections, GDK operators, and classes for the JVM dynamic language.

2 PagesIntermediateMar 30, 2026

Basics

Variables, strings, and optional typing.

groovy
// Variables and string interpolationdef name = "World"println "Hello, ${name}!"int x = 42String s = "Value: $x"// Optional typingdef add(a, b) { a + b }println add(2, 3)

Closures

Groovy's first-class blocks of code.

groovy
def square = { x -> x * x }println square(5)def greet = { String n -> "Hi, $n" }println greet("Groovy")[1, 2, 3].each { println it }        // 'it' = implicit single paramdef doubled = [1, 2, 3].collect { it * 2 }

Collections

Lists, maps, and functional-style methods.

  • [1, 2, 3]- List literal
  • [a: 1, b: 2]- Map literal
  • list.each { }- Iterate over elements
  • list.collect { }- Transform each element, like map()
  • list.findAll { it > 1 }- Filter elements matching a condition
  • list.inject(0) { acc, v -> acc + v }- Reduce/fold with an accumulator

Language Features

Groovy-specific operators and idioms.

  • def- Declares a dynamically-typed variable
  • ?.- Safe navigation operator, avoids NullPointerException
  • ?:- Elvis operator, provides a default when the left side is falsy/null
  • ==- Calls .equals() (structural equality), unlike Java's ==
  • GString- Double-quoted string supporting ${} interpolation
  • @groovy.transform.Immutable- Annotation that generates an immutable class

Classes

Defining a class with a map-based constructor.

groovy
class Person {    String name    int age    String greet() {        "Hi, I'm ${name}, ${age} years old"    }}def p = new Person(name: "Ada", age: 30)   // named-arg constructorprintln p.greet()
Pro Tip

Use the Elvis operator (value ?: default) instead of a verbose ternary null-check — it's idiomatic Groovy and reads far more cleanly.

Was this cheat sheet helpful?

Explore Topics

#Groovy#GroovyCheatSheet#Programming#Intermediate#Closures#Collections#LanguageFeatures#Classes#OOP#Functions#DataStructures#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet