open book lot

Apache Groovy 轻松列表

掌握使用 Groovy 列表功能进行数据操作。️Groovy 简化了复杂任务,使数据整理变得…
首页 » 博客 » Apache Groovy 轻松列表

Groovy 的列表超越了 Java 的基本功能,提供了丰富的语法和方法,可轻松进行数据操作。灵活的类型、 直观的运算符 和强大的方法让您掌控数据。

这远远超出了 java.util.List 接口及其各种实现提供的基本功能,这些功能已经非常酷了。  (如果您尚未安装 Groovy,请 阅读本系列的介绍 系列。)

开始研究 Groovy 中的 List 的一个好地方是一些基本的语法元素。以下是一些

1  def testList = []
2  testList << 1
3  testList << "Hi there"
4  testList << java.time.LocalDate.of(2023,03,01)
5  testList[15] = 2
6  testList[-2] = 1.5
7  testList << [3,4,5]
8  println "testList $testList"
9  println "testList.class ${testList.class}"
10 println "2 in testList ${2 in testList}"

第 1 行定义了变量 testList 并将其设置为空列表。

第 2-4 行附加了 int (Integer)、Stringjava.time.LocalDate 的实例,表明 Groovy 对特定类型的列表是不可知的。

第 5 行将 int (Integer) 值 2 粘在位置 15(列表的第十六个元素)中。这会产生副作用,即在列表中从位置 3 到 14 插入 null。

第 6 行使用负索引,Groovy 将其解释为从右侧计数,以将十进制 (BigDecimal) 数字 1.5 插入倒数第二个列表元素:-1 是最右边的元素。我认为这些负索引是从列表的长度中减去以获得相关列表元素的数字。

第 7 行将列表 [3, 4, 5] 附加到列表,使其成为子列表。

第 8 行打印出列表,第 9 行告诉您您创建了什么样的野兽。

第 10 行使用 in 运算符来确定是否有任何元素的值为 2。

运行此脚本会产生

$ groovy Groovy12a.groovy
testList [1, Hi there, 2023-03-01, null, null, null, null, null, null, null, null, null, null, null, 1.5, 2, [3, 4, 5]]
testList.class class java.util.ArrayList
2 in testList true

您应该注意,上面提到的空值和子列表中的空值都在最后一个元素中。此外,如您所知,列表的倒数第二个元素是 2,因此 in 运算符产生 true。

Groovy 向 List 接口添加了许多方法。需要记住的一个重要事项是,其中一些方法会改变列表,而另一些方法则会从旧列表中创建一个新列表,应用修改。

例如,plus()(可以通过 + 运算符访问)创建一个新列表,该列表是使用该方法(或加号运算符)连接两个列表的结果。使用 minus() 通过删除第二个项目或列表的所有实例来生成新列表。而 removeAt() 通过删除其参数给出的位置的元素来修改列表

1  def l1 = 'Lists are where Groovy gets way more interesting than the base functionality provided by the java.util.List interface and its various implementations, which are already pretty cool.'.split() as List
2  def l2 = 'A good place to start looking at List in Groovy is some basic syntactical elements.'.split() as List
3  def l3 = l1 + l2
4  def l4 = l1 - 'the'
5  def l5 = l2.clone()
6  l5.removeAt(9)
7  println "l1 $l1"
8  println "l2 $l2"
9  println "l3 $l3"
10 println "l4 $l4"
11 println "l5 $l5"

第 1-2 行通过将 split() 函数应用于本文的第二句和第三句,创建两个数组,然后应用 as 运算符将这些数组转换为 List 实例,从而创建两个列表 l1l2

第 3 行应用 + 运算符(即 plus() 方法)来创建一个新列表 l3,它是两个列表 l1l2 的连接。

第 4 行应用 - 运算符(即 minus() 方法)来创建一个新列表 l4,它是列表 l1,其中删除了所有等于“the”的元素。

第 5-6 行使用 clone() 方法创建 l5 作为 l2 的副本,并从 l5 中删除第 9 个元素。请注意,如果您只是将 l2 分配给 l5,则两个变量都将指向同一个列表。

第 7-11 行打印列表。

当您运行此代码时,您会看到

$ groovy Groovy12b.groovy
l1 [Lists, are, where, Groovy, gets, way, more, interesting, than, the, base, functionality, provided, by, the, java.util.List, interface, and, its, various, implementations,, which, are, already, pretty, cool.]
l2 [A, good, place, to, start, looking, at, List, in, Groovy, is, some, basic, syntactical, elements.]
l3 [Lists, are, where, Groovy, gets, way, more, interesting, than, the, base, functionality, provided, by, the, java.util.List, interface, and, its, various, implementations,, which, are, already, pretty, cool., A, good, place, to, start, looking, at, List, in, Groovy, is, some, basic, syntactical, elements.]
l4 [Lists, are, where, Groovy, gets, way, more, interesting, than, base, functionality, provided, by, java.util.List, interface, and, its, various, implementations,, which, are, already, pretty, cool.]
l5 [A, good, place, to, start, looking, at, List, in, is, some, basic, syntactical, elements.]

另一种语法支持来自在方括号内使用范围来引用 List 实例的子列表或切片

1  def l1 = [0,1,2,3,4,5,6,7,8,9]
2  def l2 = l1[3..5]
3  println "l1 $l1"
4  println "l2 $l2"

当您运行此代码时,您会看到

$ groovy Groovy12c.groovy
l1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l2 [3, 4, 5]

添加到 List 接口的许多非常酷的方法都使用 Closure 参数来对列表执行有趣的操作。您将在即将发表的关于闭包的文章中看到其中一些内容。

对于 LISP 爱好者,Groovy List 提供了 head()tail() 方法,它们分别实现了 LISP 操作 carcdr

这是一个很好的建议,可以转到 Groovy List 接口参考描述,并阅读 关于 List 的 Groovy 文档

结论

Groovy 将普通的 Java List 实现提升到接近卓越的水平。Groovy 通过添加许多有用的方法并提供运算符来实现这一点,这些运算符以更易读和更紧凑的方式使用其中一些常用方法。

与 Java 的一个主要区别是,Groovy 不会从 List 实例应包含相同类型元素这一角度进行操作。当类型检查未启用时,Groovy 不会强制执行元素类型。例如

1  def l = new ArrayList<Integer>()
2  l << 1
3  l << 2
4  l << "3"
5  println l

它可以很好地编译和执行

$ groovy Groovy12e.groovy
[1, 2, 3]

请继续关注本系列的下一期,我们将研究闭包。

作者

如果您喜欢这篇文章,您可能还会喜欢这些