0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · บทเรียน

การปรับปรุงคอลเลกชันของ Groovy

ค้นพบส่วนเสริมอันทรงพลังของ Groovy ที่มีต่อคอลเลกชัน Java ซึ่งช่วยให้การจัดการข้อมูลง่ายและสื่อความหมายได้มากขึ้น

การปรับปรุงคอลเลกชันของ Groovy เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Groovy & Gradle: JVM Automation and Build Engineering หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Groovy & Gradle: JVM Automation and Build Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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