Playing with groovy - Compare two Collections of String

I was wondering what if I need to compare two collections of String with the popular language called “groovy”. I played with it sometimes through Grails but never for ultra simple task. You can read but be careful and don’t use it as a reference. I am not an expert ( for the moment … ).

def codesA = ['ABA', 'FRS', 'ARA', 'UKA', 'VNC']
def codesB = ['XIX', 'ARA', 'VNC', 'ARS', 'ERA']
def equalCodes = [] as Set
def compareTwoLists = {left, right -> left.each({ l-> right.each( {r -> if(l==r) equalCodes.add(l)}) }) }
compareTwoLists(codesA, codesB)
println(equalCodes)

Closure are powerful but can be kind of unreadable … more here.

Playing with groovy - Compare two Collections of String

I was wondering what if I need to compare two collections of String with the popular language called “groovy”. I played with it sometimes through Grails but never for ultra simple task. You can read but be careful and don’t use it as a reference. I am not an expert ( for the moment … ).

def codesA = ['ABA', 'FRS', 'ARA', 'UKA', 'VNC']
def codesB = ['XIX', 'ARA', 'VNC', 'ARS', 'ERA']
def equalCodes = [] as Set
def compareTwoLists = {left, right -> left.each({ l-> right.each( {r -> if(l==r) equalCodes.add(l)}) }) }
compareTwoLists(codesA, codesB)
println(equalCodes)

Closure are powerful but can be kind of unreadable … more here.

Posted 2 years ago & Filed under dev,

About:

Following: