[英]Is this an usage of Groovy collect closure abbreviation?
I study the Groovy code which deals with JSON parsing and find the following strange code that I cannot understand:我研究了处理 JSON 解析的 Groovy 代码,发现以下我无法理解的奇怪代码:
list.groovy列表.groovy
def lst = [
[name: 'Bill', age: 30],
[name: 'Jobs', age: 40],
[name: 'Steve', age: 50],
]
println lst
println lst.name
println lst.collect{it.name}
println lst.age
println lst.agenda
Platform: Windows 10, Groovy version: 2.5.9平台:Windows 10、Groovy 版本:2.5.9
C:\chenchang\study\groovy_study\closure>groovy -version
Groovy Version: 2.5.9 JVM: 1.8.0_272 Vendor: AdoptOpenJDK OS: Windows 10
The output of the Groovy script: Groovy脚本的output:
C:\chenchang\study\groovy_study\closure>groovy list.groovy
[[name:Bill, age:30], [name:Jobs, age:40], [name:Steve, age:50]]
[Bill, Jobs, Steve]
[Bill, Jobs, Steve]
[30, 40, 50]
[null, null, null]
I cannot understand lst.name
part.我无法理解lst.name
部分。 It looks like lst.collect{it.name}
but I cannot find any reference documentation or formal explanations.它看起来像lst.collect{it.name}
但我找不到任何参考文档或正式解释。 Can anyone explain what Groovy syntax it is?谁能解释它是什么 Groovy 语法?
After reviewed your comments, I update the sample code and find some new knowledge that I don't know before.在查看了您的评论后,我更新了示例代码,并发现了一些我以前不知道的新知识。
def lst = [
[name: 'Bill', age: 30],
[name: 'Jobs', age: 40],
[name: 'Steve', age: 50],
null, // I add a null value in the list
]
println lst
println lst.name
println lst.findAll{ it != null }.collect{ it.name }
println lst*.name
/* println lst.collect{ it.name } */
/* java.lang.NullPointerException: Cannot get property 'name' on null object */
println lst.collect{ it?.name }
The output of the revised Groovy script:修改后的Groovy脚本的output:
C:\chenchang\study\groovy_study\closure>groovy list.groovy
[[name:Bill, age:30], [name:Jobs, age:40], [name:Steve, age:50], null]
[Bill, Jobs, Steve]
[Bill, Jobs, Steve]
[Bill, Jobs, Steve, null]
[Bill, Jobs, Steve, null]
lst.name
is GPath which ignores the null values. lst.name
是忽略null值的 GPath。 So the length of the returned new list will be less than the length of the original list if the original list contains null values.因此,如果原始列表包含 null 值,则返回的新列表的长度将小于原始列表的长度。 lst.name
is equivalent to lst.findAll{ it.= null }.collect{ it.name }
. lst.name
等价于lst.findAll{ it.= null }.collect{ it.name }
。
lst*.name
is spread dot operator
or Star dot operator . lst*.name
是spread dot operator
或星点运算符。 The length of the returned new list is equal to the length of the original list.返回的新列表的长度等于原始列表的长度。 If some values in the original list are null
, the corresponding values in the new list will be null and no NullPointerException
will be thrown.如果原始列表中的某些值为null
,则新列表中的对应值为 null 并且不会抛出NullPointerException
。 lst*.name
is equivalent to lst.collect{ it?.name }
. lst*.name
等价于lst.collect{ it?.name }
。
def lst = []
is a java.util.ArrayList def lst = []
是java.util.ArrayList
you can use println lst.getClass()
to check it你可以使用println lst.getClass()
来检查它
ArrayList
implements Iterable
, Collection
, ... and other interfaces ArrayList
实现了Iterable
, Collection
,...等接口
groovy could add functionality on any level of super interface or super class. groovy 可以在任何级别的超级接口或超级 class 上添加功能。
groovy gdk documentation explains additional functionality that groovy adds to java groovy gdk 文档解释了 groovy 添加到 java 的附加功能
to get list of all available methods in groovy you have to check java doc and all super classes/interfaces of groovy doc.要获取 groovy 中所有可用方法的列表,您必须检查 java 文档和 groovy 文档的所有超类/接口。
in case of lst.name
groovy actually calls lst.getAt('name')
如果是lst.name
groovy 实际上调用lst.getAt('name')
getAt(String key) - you can find in groovy doc Collection getAt(String key) - 你可以在 groovy doc Collection中找到
and finally here is a doc about groovy collections: http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html and finally here is a doc about groovy collections: http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.