Groovy web console

subscribe to the feed Subscribe
to this
merge lists (via #groovywebconsole)
tweet this snippet Tweet

merge lists

Published 1 year ago by nmrao with tags merge
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def list1 = [[project_no:'0-10060', case_no:'01757560'], [project_no:'1-153246461', case_no:'02039960'], [project_no:'1-175858654', case_no:'01995271'], [project_no:'1-224323229', case_no:'01912596'], [project_no:'1-228256337', case_no:'02039596'], [project_no:'1-53795134', case_no:'02029418'], [project_no:'2-00073626', case_no:'01998455']]
def list2 = [[project_no:'0-10060', case_no:'01757560'], [project_no:'1-224323229', case_no:'01912596'], [project_no:'1-228256337', case_no:'02039596'], [project_no:'1-233412391', case_no:'02060631']]

def result = list1+list2
def projNos = result.inject([]) { keys, item ->  keys << item.project_no } as Set

def finalResult = []
projNos.each { proj ->
    //To avoid duplicate cases in project, using it as Set
    def cases = [] as Set
    result.each { item ->
        if (item.project_no == proj) {
            cases << item.case_no
    finalResult << [ 'project_no': proj, 'case_no' : cases ]

println finalResult