1 问题
- 使用collect 获取List的属性集合时,会存在null元素
- 这不是我想要的,groovy似乎没有lamda表达式的写法
- 定义变量,通过循环添加又不是我想要的,感觉不是很优雅
2 可行写法
- lamda加上了groovy的collect方法
persons.stream().filter(c->{return c.name!=null;}).collect{it.name}
- 纯粹的lamda写法
persons.stream().filter(c->{return c.name != null;}).map(c-> c.name).collect(Collectors.toList())
- 总结:写法一会让代码简单一些,可能这也是groovy的一个初衷
3 终极写法
- groovy有去除null元素方法
persons.collect{it.name}.grep()
4 极限写法
- groovy collect本身有简写
persons.name.grep()