GDK Extensions to Object Add comment to Wiki View in Wiki Edit Wiki page Printable Version

Groovy adds a number of methods to java.lang.Object, most of which deal with types that serve as collections or aggregates, such as Lists or DOM Nodes.

Return Value

Method

Description

Boolean

any {closure}

returns true if the closure returns true for any item

List

collect {closure}

returns a list of all items that were returned from the closure

Collection

collect(Collection collection) {closure}

same as above, but adds each item to the given collection

void

each {closure}

simply executes the closure for each item

void

eachWithIndex {closure}

same as each{} except it passes two arguments: the item and the index

Boolean

every {closure}

returns true if the closure returns true for all items

Object

find {closure}

returns the first item that matches the closure expression

List

findAll {closure}

returns all items that match the closure expression

Integer

findIndexOf {closure}

returns the index of the first item that matched the given expression

See the GDK documentation on Object for the complete list of added methods.

Since the "return" keyword is optional in Groovy, closures in this context act as "predicates" and return the boolean result of whatever expression you given in your closure. These predicates allow you to apply perform operations on aggregate objects in a very concise manner.

Examples

 

Search

Results of your search request can come from various sources: the Groovy website itself, the JIRA issues, the API documentation, as well as a few other interesting Groovy-related blogs.

  By  -  pages  -  views  - last modified