【问题标题】:Is there any way to compare array list using contains with multiple condition, code looks like this有没有办法使用包含多个条件来比较数组列表,代码如下所示
【发布时间】:2019-04-16 08:03:17
【问题描述】:
ense.findAll { obj ->
    obj.otherDetails.findAll { od ->
        od.activityType.name.contains('Exporter') && od.activityType.name.contains('Distribution')
    }
}

此代码的预期输出必须检索所有活动类型为 Exporter && Distribution 的 ense

例如

ense 1 has [Exporter, Distribution]
ense 2 has [Hauler]
ense 3 has [Distribution, Exporter]
ense 4 has [Exporter, Refiller]

那么上面的代码必须只检索 ense 1 和 ense 3

【问题讨论】:

  • 请提供您的问题的完整示例,或者至少提供您得到的输出而不是预期的输出。
  • 已经更新,谢谢
  • 我认为很清楚,你想要什么,你的代码看起来并不那么糟糕。那么你得到什么输入的结果是什么?请在问题中添加您的实际输入或一些简化的子集,以显示实际问题。
  • 什么是“感觉”?为什么您的问题有不必要的代码?简化代码和案例,这样问题就会更短且不言自明。据我了解,只是在您的复杂对象中移动的两个 findAll 对您的问题毫无用处,但您只是复制了之前的一些代码......
  • ` ense 4 有 [Exporter, Refiller]` 听起来好像当前包含正在返回 true/false,但您实际上想要一个包含 ense 对象映射的列表,然后是它可能具有的任何 activityType,尝试def myResults=ense.collect{[it:it.otherDetails.activityType]}?.flatten() 然后myResults?.each { k,v -> println "${k} = ense object in its whole and value ${v} is a collection or a list of all of its activity types" }

标签: grails groovy


【解决方案1】:

如果你只想有一个句子,只需使用正则表达式。在 groovy 中,您可以像这样检查正则表达式:

def list = ['a','b','c','d']

list.each{
    println it ==~ /a|b/ 
}

所以 ==~ 将检查正则表达式并返回一个布尔值,输出将是:

true
true
false
false

or 在正则表达式中作为 |,您可以将正则表达式替换为您想要的逻辑(这在问题中确实不清楚)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 2021-08-02
    • 1970-01-01
    • 2021-09-13
    • 2021-01-01
    • 2021-05-01
    • 1970-01-01
    相关资源
    最近更新 更多