Groovy GDK Guide — Enhanced Standard Library and Collection Methods
In this tutorial, you will learn about Groovy GDK Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
The Groovy Development Kit (GDK) adds dozens of convenience methods to Java's standard classes -- enabling operations like .each(), .collect(), .findAll(), and .inject() on collections, strings, numbers, and files with minimal code.
Collection Enhancements
def numbers = [4, 2, 8, 1, 6, 3]
// Aggregation
println numbers.sum() // 24
println numbers.max() // 8
println numbers.min() // 1
println numbers.average() // 4.0
println numbers.count { it > 3 } // 3
// Filtering
println numbers.findAll { it % 2 == 0 } // [4, 2, 8, 6]
println numbers.grep { it > 3 } // [4, 8, 6]
println numbers.find { it > 5 } // 8 (first match)
// Transformation
println numbers.collect { it * 2 } // [8, 4, 16, 2, 12, 6]
println numbers.collectMany { [it, it] } // flatten map
String Enhancements
def str = " Hello World "
// Trimming
println str.trim() // "Hello World"
println str.strip() // "Hello World" (Unicode-aware)
// Padding
println "42".padLeft(5) // " 42"
println "42".padRight(5) // "42 "
// Tokenizing
println str.tokenize() // [Hello, World]
println "a,b,c,d".split(",") // [a, b, c, d]
// Other
println "hello".capitalize() // Hello
println str.contains("World") // true
println "test".center(10) // " test "
File Enhancements
// Read entire file
def content = new File("data.txt").text
println content
// Write entire file
new File("output.txt").text = "Hello Groovy!"
// Process each line
new File("log.txt").eachLine { line ->
println "Line: $line"
}
// Process with filter
new File("data.txt").filterLine { it =~ /ERROR/ }
Number Enhancements
// Ranges
def range = 1..10
println range.sum() // 55
println range.average() // 5.5
// Iteration
10.times { print "$it " } // 0 1 2 3 4 5 6 7 8 9
1.upto(5) { print "$it " } // 1 2 3 4 5
5.downto(1) { print "$it " } // 5 4 3 2 1
Common Mistakes
1. Using .each when .collect is needed
.each returns the original collection. .collect returns a new transformed collection.
2. Not knowing GDK methods exist
Java developers often write loops where GDK methods like .findAll, .collect, and .each would be more concise.
3. Chaining too many methods
While chainable, excessive chaining can hurt readability. Extract intermediate variables for clarity.
FAQ
{{< faq question="Can I add my own methods to the GDK?" >}}
Yes, through Groovy's metaclass system. Use String.metaClass.myMethod = { ... } to add methods to existing classes.
{{< /faq >}}
{{< faq question="Are GDK methods available in Java?" >}} No. GDK is a Groovy-only feature. But Groovy code with GDK methods compiles to Java bytecode. {{< /faq >}}
{{< faq question="Is there a performance cost to GDK?" >}} Negligible for typical usage. GDK methods use Java's standard APIs internally with minimal overhead. {{< /faq >}}
What's Next
Now learn about Groovy closures.
| Topic | Description | Link |
|---|---|---|
| Closures | Anonymous functions | {{< ref "03-closures" >}} |
| GPars | Parallel processing | {{< ref "04-gpars" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro