Search

Dark theme | Light theme

October 5, 2009

Groovy Goodness: GroovyCollections Utility Class

Groovy has a utility class to make combinations of lists or arrays, or to tranpose lists or arrays: GroovyCollections. The class also contains min(), max() and sum() methods, but these are already available as methods on lists and arrays. Let's see the class in action:

def methods = ['min', 'max', 'sum']
def classes = ['Object[]', 'Collection']

def combinations = GroovyCollections.combinations([classes, methods])
assert 2 * 3 == combinations.size()
assert [['Object[]', 'min'], ['Collection', 'min'], ['Object[]', 'max'], ['Collection', 'max'], ['Object[]', 'sum'], ['Collection', 'sum']] == combinations
assert 3 == combinations.findAll { it[0] == 'Collection' }.size()

def keys = ['username', 'email']
def values = ['mrhaki', 'email@host.com', 'not-tranposed']
def transpose = GroovyCollections.transpose([keys, values, [0, 1]])
assert 2 == transpose.size()
assert [['username', 'mrhaki', 0], ['email', 'email@host.com', 1]] == transpose