我有一个收藏,我想找到某些元素并对其进行改造。我可以在两个闭包中做到这一点,但我想知道是否可以只使用一个?
def c = [1, 2, 3, 4]
def result = c.findAll {
it % 2 == 0
}
result = result.collect {
it /= 2
}
我真正的用例是使用 Gradle,我想找到一组特定的文件并将它们转换为完全限定的包名称。
您可以使用findResults
:
def c = [1, 2, 3, 4]
c.findResults { i ->
i % 2 == 0 ? // if this is true
i / 2 : // return this
null // otherwise skip this one
}
此外,[]
如果没有任何元素满足标准(关闭) ,您将得到