April 27, 2011

Groovy Goodness: Check if Maps are Equal

With Groovy 1.8 the equals() method is added to Map. This means we can check if maps are equals. They are equals if both maps have the same size, and keys and values are the same.

def map1 = [user: 'mrhaki', likes: 'Groovy', age: 37]
def map2 = [age: 37.0, likes: 'Groovy', user: 'mrhaki']
def map3 = [user: 'Hubert Klein Ikkink', likes: 'Groovy']

assert map1.equals(map2)
assert map1 == map2
assert !map1.equals(map3)
assert map2 != map3