Groovy 中是否有 Scala 'zip' 函数的模拟? [英] Is there any analog for Scala 'zip' function in Groovy?

查看:10
本文介绍了Groovy 中是否有 Scala 'zip' 函数的模拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Scala 中,我可以这样写:

In Scala I can write something like this:

val a = List(1, 2, 3)
val b = List(4, 5)
println(a zip b)

这将产生 List((1,4), (2,5)) 作为输出.
现在我在 Groovy 中有两个集合,并希望以类似的方式压缩它们.最简单的方法是什么?

That would produce List((1,4), (2,5)) as output.
Now I have two collections in Groovy and want to zip them in similar fashion. What is the simplest way to do this?

推荐答案

Groovy 相当于 Scala 的 zipList#transpose,可以在列表列表上调用:

Groovy's equivalent of Scala's zip is List#transpose, which can be called on a list of lists:

assert [[1, 2, 3], [4, 5]].transpose() == [[1, 4], [2, 5]]

这篇关于Groovy 中是否有 Scala 'zip' 函数的模拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆