Dark theme | Light theme

March 9, 2010

Groovy Goodness: Use Strings as Subscript Operator

Groovy adds the method getAt(String) to the Collection class. We can use it to request property values from elements in a list.

class User { String name }
def list = [new Date(), new User(name: 'mrhaki'), 42.0, 'Groovy Rocks!']

assert ['java.util.Date', 'User', 'java.math.BigDecimal', 'java.lang.String'] == list['class']['name']