0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · 课时

Groovy 集合增强功能

了解 Groovy 为 Java 集合提供的强大扩展,让数据操作更加简单且富有表现力。

Groovy 集合增强功能 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Groovy & Gradle: JVM Automation and Build Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Groovy's Collection Power-Up

Groovy supercharges Java's standard collections (List, Map, Set) with many new, convenient methods. This makes your code shorter, more readable, and much more expressive.

You'll spend less time writing boilerplate and more time solving problems!

Easy List Initialization

Creating and populating lists in Groovy is a breeze. No need for new ArrayList<>() or repetitive add() calls. Just use square brackets []!

class Main {
  static void main(String[] args) {
    def numbers = [1, 2, 3, 4, 5]
    def names = ["Alice", "Bob", "Charlie"]
    println "Numbers: " + numbers
    println "Names: " + names
  }
}

The Versatile `each` Method

The each method is your go-to for iterating over collections. It's concise and works with both Lists and Maps.

For lists, it's like a simplified loop. For maps, it iterates over entries.

class Main {
  static void main(String[] args) {
    def fruits = ["Apple", "Banana", "Cherry"]
    fruits.each { fruit ->
      println "I love " + fruit
    }
  }
}

Transforming Lists with `collect`

The collect method creates a new list by transforming each element of the original list. It's perfect for mapping items to new values.

class Main {
  static void main(String[] args) {
    def numbers = [1, 2, 3]
    def doubledNumbers = numbers.collect { it * 2 }
    println "Original: " + numbers
    println "Doubled: " + doubledNumbers
  }
}

Filtering Elements with `findAll`

Need to select elements that meet specific criteria? Use findAll. It returns a new list containing only the elements for which the closure evaluates to true.

class Main {
  static void main(String[] args) {
    def ages = [12, 18, 25, 7, 30]
    def adults = ages.findAll { it >= 18 }
    println "Ages: " + ages
    println "Adults: " + adults
  }
}

Counting Matching Elements

The count method helps you quickly determine how many elements in a collection satisfy a given condition. It returns a single integer.

class Main {
  static void main(String[] args) {
    def scores = [85, 92, 78, 95, 88]
    def highScores = scores.count { it > 90 }
    println "Scores: " + scores
    println "Number of high scores (>90): " + highScores
  }
}

Simplified Map Syntax

Groovy makes creating maps incredibly easy using square brackets [] and colons : for key-value pairs. Keys can often be unquoted strings.

class Main {
  static void main(String[] args) {
    def person = [name: "Anna", age: 30, city: "New York"]
    println "Person: " + person
    println "Name: " + person.name // Property-style access
    println "Age: " + person["age"] // Map-style access
  }
}

Iterating Over Maps

Just like lists, maps can be iterated using each. The closure for maps typically takes two arguments: key and value.

You can also use eachWithIndex if you need the index.

class Main {
  static void main(String[] args) {
    def config = [mode: "dev", port: 8080]
    config.each { key, value ->
      println "${key}: ${value}"
    }
  }
}

Accessing Properties with Spread `*.`

The spread operator *. is a powerful Groovy feature for collections. It applies a property access or method call to all elements in a collection, returning a new list of results.

class Person {
  String name
  int age
}

class Main {
  static void main(String[] args) {
    def people = [
      new Person(name: "Alice", age: 25),
      new Person(name: "Bob", age: 30)
    ]
    def names = people*.name
    println "People names: " + names
  }
}

Collection Challenge

Consider the following Groovy code snippet:

def items = [10, 25, 30, 45, 50]
def result = items.findAll { it > 20 }.collect { it / 5 }
println result

What will be the output of this code?

Recap: Groovy's Collection Power

You've explored how Groovy significantly enhances standard Java collections. We covered:

  • Simplified List and Map creation.
  • Powerful iteration with each.
  • Transforming collections with collect.
  • Filtering with findAll and counting with count.
  • The convenient Spread Operator *. for bulk property access.

These features make working with data structures in Groovy much more efficient and enjoyable!

常见问题解答

「Groovy 集合增强功能」课时是免费的吗?

是的 — 「Groovy 集合增强功能」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

「Groovy 集合增强功能」这节课中我会学到什么?

了解 Groovy 为 Java 集合提供的强大扩展,让数据操作更加简单且富有表现力。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「Groovy 集合增强功能」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?

能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Groovy 集合增强功能
  2. 理解 Groovy 闭包
  3. 使用 Groovy 实现函数式模式
  4. 闭包的柯里化与组合
← 返回 Groovy & Gradle: JVM Automation and Build Engineering