Search

Dark theme | Light theme

April 27, 2011

Groovy Goodness: Group and Count Elements in Collection or Map

Since Groovy 1.8 we can group items in a collection, map or array and count the number of elements in a group. We use a closure to define the keys for the grouping. Then the number of elements in the group are counted and that is the value of the grouping key.

def list = ['Groovy', 'Grails', 'Java']
assert list.countBy { it[0] } == ['G': 2, 'J': 1]  // 2 items start with G, 1 with J.

def numbers = [1,2,3,4,5] as Integer[]
assert numbers.countBy { it % 2 } == [0: 2, 1: 3]  // 2 even, 3 uneven numbers

def map = [user: 'mrhaki', city: 'Tilburg', age: 37]
assert map.countBy { key, value -> value.class } == [(String.class): 2, (Integer.class): 1]  // 2 values of type String and 1 of type Integer