taiyebur taiyebur - 1 year ago 82
Groovy Question

Create a list of lists from two maps with values of the common keys

There are two different maps. One is bigger than the other. The keys of small map is always a subset of keys of the bigger map. I want to take the values for keys that are common to both maps and create a list of lists using Groovy features. The objective is to achieve it with as little code as possible with Groovy features.

Map big = ['FirstName':'first_name', 'LastName':'last_name', 'FullName':'full_name']
Map small = ['FirstName':'John', 'FullName':'John Williams']

println Output

[[first_name, John], [full_name, John Williams]]

Answer Source
def result = []
big.keySet().intersect(small.keySet()).each {
    result << [big[it], small[it]]

assert [['first_name', 'John'], ['full_name', 'John Williams']] == result
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download