0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · 강의

Groovy 컬렉션 확장 기능

Java 컬렉션에 추가된 Groovy의 강력한 기능을 살펴보고 데이터 조작을 더 간단하고 표현력 있게 수행합니다.

Groovy 컬렉션 확장 기능은(는) CoddyKit의 무료 Groovy & Gradle: JVM Automation and Build Engineering 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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 컬렉션 확장 기능” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Groovy & Gradle: JVM Automation and Build Engineering 강의 전체를 잠금 해제할 수 있습니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.

“Groovy 컬렉션 확장 기능”에서 뭘 배우나요?

Java 컬렉션에 추가된 Groovy의 강력한 기능을 살펴보고 데이터 조작을 더 간단하고 표현력 있게 수행합니다. 브라우저에서 직접 실행하는 실습 코드로 Groovy & Gradle: JVM Automation and Build Engineering을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Groovy & Gradle: JVM Automation and Build Engineering을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Groovy & Gradle: JVM Automation and Build Engineering은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“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(으)로 돌아가기