简体   繁体   English

比较两个列表时发生奇怪的情况(常规)

[英]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? 它永远不会迭代到for循环吗? 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. list1list列表。 So, you should iterate each item again after fetching from list1 因此,您应该在从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] 您的示例的结果将是:[GNCSSTDI,LMGQYNQU]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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