August 9, 2010

Groovy Goodness: Subtracting Map Entries

Groovy 1.7.4 adds the minus() method to the Map class. The result is a new map with the entries of the map minus the same entries from the second map.

def m1 = [user: 'mrhaki', age: 37]
def m2 = [user: 'mrhaki', name: 'Hubert']
def m3 = [user: 'Hubert', age: 37]

assert [age: 37] == m1 - m2
assert [user: 'mrhaki'] == m1 - m3