Groovy:为List中的每个String添加前缀

Mat*_*ias 5 groovy

给出以下列表:

List<String> list = ["test1", "test2", "test3"]
Run Code Online (Sandbox Code Playgroud)

现在我想以下列格式从这个列表中创建一个String:

"pre/test1 pre/test2 pre/test3"
Run Code Online (Sandbox Code Playgroud)

所以我想我会采取以下方式:

println list.each { "pre/$it" }.join(' ')
Run Code Online (Sandbox Code Playgroud)

但是,这会导致以下输出:

"test1 test2 test3"
Run Code Online (Sandbox Code Playgroud)

(注意缺少的前缀.)
如何在Groovy中实现所需的字符串连接?

Opa*_*pal 12

def joined = ["test1", "test2", "test3"].collect { "pre/$it" }.join(' ')
Run Code Online (Sandbox Code Playgroud)

each返回未修改的集合 - 而collect返回包含已修改内容的集合.