加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy的非参数grep()和findAll()方法有什么不同吗?

发布时间:2020-12-14 16:37:53 所属栏目:大数据 来源:网络整理
导读:从Groovy JDK: public Collection grep() Iterates over the collection of items which this Object represents and returns each item that matches using the IDENTITY Closure as a filter – effectively returning all elements which satisfy Groovy
从Groovy JDK:

public Collection grep()

Iterates over the collection of items which this Object represents and returns each item that matches using the IDENTITY Closure as a filter – effectively returning all elements which satisfy Groovy truth.

public Collection findAll()

Finds all items matching the IDENTITY Closure (i.e. matching Groovy truth).

解决方法

简答:结果将一样。

长答案:
grep通常使用一个过滤器对象,然后调用isCase。因此,grep的参数通常不是Groovy Closure。对于findAll,您可以使用Closure作为参数,并且如果将Closure的结果评估为true,则将其转入生成的集合。

现在重要的是要知道Closure还有一个isCase方法。 Closure#isCase(Object)将使用参数作为Closure的参数执行Closure,然后使用Groovy Truth对其结果进行评估。对于身份关闭,即{it},这意味着关闭将返回给它的内容,因此Groovy将把Groovy Truth应用于grep调用的参数。结果与findAll相同。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读