Search

Dark theme | Light theme

April 3, 2011

Groovy Goodness: See if List and Object Array are Equal

Groovy adds a lot of useful methods to standard JDK classes. For example Groovy adds the equals() method to List and Object[] so both can be compared. We must make sure the array is of type Object[] to make it work. Also the equals() method is added to arrays of type int.

def numbers1 = [1,2,3] as int[]
def numbers2 = [1,2,3] as int[]
def numbers3 = [1,2] as int[]

assert numbers1.equals(numbers2)
assert numbers2 == numbers1
assert !(numbers1.equals(numbers3))

def list = ['Groovy', 'Grails', 'Gradle']
def stringArray1 = ['Grails', 'Gradle', 'Groovy'] as Object[]
def stringArray2 = ['Groovy', 'Grails', 'Gradle'] as Object[]

assert list.equals(stringArray2)
assert list == stringArray2
assert !(list.equals(stringArray1)) // order matters
assert list != stringArray1