0

I have 3 lists that I need to compare and manipulate. Here are the lists, for example:

def list1 = ['abc', '123','789'];
def list2 = ['456', 'abc', '123'];
def list3 = ['mil', 'len', 'nium']; 

I need to compare values of list1 and list2 and if they are the same, I need to return the corresponding item with the same index of list1 found in list3.

Using the example above, since list1[0] and list2[1] are the same, I need to return the list3 value, 'mil'.

The lists' value will also be dynamic so how will I be able to do it in groovy?

jenniemae
  • 9
  • 1

1 Answers1

0

You can read about it on Groovy Docs. You can find common elements by

def commons = collection1.intersect(collection2)

and find element in third list using index from first list by indexOf. It may be useful as well Comparing lists

Something like

def commons = list1.intersect(list2)
def indexes = []
commons.each {
    indexes << list1.indexOf(it)
}
def values = []
indexes.each {    
    values << list3[it]
}

Of course it can be done in many ways, easier, but it works too in some cases

SURU
  • 178
  • 1
  • 1
  • 11