简体   繁体   中英

Weird occurrence happening when comparing two list (groovy)

I am trying to scan one lists contents if the other list also contains a similar item.

I am also not sure if this is the most effective way to go through the list from what I am currently using.

Code below that I'm trying to run:

List list1 = [["GNCSSTDI", "Joe", "Thu Mar 07 19:43:59 EST 2019", "Logged work on 3/7"], ["LMGQYNQU", "Joe", "Thu Mar 07 21:41:50 EST 2019", "logged 3/7"]]

List list2 = ["GNCSSTDI", "LMGQYNQU", "AEIOSJWP"]

list1.each { l1 ->
    list2.each { l2 ->
        if (list1.toString() == l2.toString())
        {
            // match found
            log.debug("MATCH FOUND")
        }
    }
}

It never iterates to that for loop? What exactly am I doing wrong. The first item from each list should have matched if I'm not mistaken?

Thanks

list1 is list of lists. So, you should iterate each item again after fetching from list1

list1.each { l1 ->
    l1.each { subL1 ->
        list2.each { l2 ->
            if (subL1.toString() == l2.toString()) {
                // match found
                println "match found"
            }
        }
    }
}

Depending on what you want to do with the results, something like this may be useful;

List matches = list1*.intersect(list2).flatten()

The result of your example would be: [GNCSSTDI, LMGQYNQU]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM