Search

Dark theme | Light theme

April 27, 2011

Groovy Goodness: See if Sets are Equal

Since Groovy 1.8 we can use the equals() method to compare the contents of two Set collections. The two sets are equals if they have the same size and all elements are in both sets.

def set1 = ['Java', 42, true] as Set
def set2 = ['Groovy', 42, true, 'Java'] as Set
def set3 = [42L, true, 'Java'] as Set

assert set1.equals(set3)
assert set1 == set3
assert !set1.equals(set2)
assert set2 != set3