用方括号[:]括起来的冒号在Java/groovy中是什么意思



在读取nextflow管道时,我遇到了这样的表示法:

channel =[:]

后来以这种方式使用:

anotherChannel.each { integer, file ->
if ( !channel.containsKey(integer) ) {
channel.put(integer, [])
}
channel.get(integer).add(file)
}

这是一些特殊的列表符号吗我需要理解它的原因是我必须向它添加另一个文件,类似于这样的文件:

anotherChannel.each { integer, file, file2 ->
if ( !channel.containsKey(integer) ) {
channel.put(integer, [])
}
channel.get(integer).add(file, file2)
}

但它显然不起作用。

关于问题的第二部分,下面创建一个带有2个参数的闭包,并将其作为参数传递给Map.each

anotherChannel.each { integer, file ->    
}

这个闭包将为anotherChannel中的每个条目调用一次,其中条目的键分配给integer,条目的值分配给file。不能只向这个闭包添加另一个参数,因为Map.each不支持3参数闭包。

我想这会做你想要的

anotherChannel.each { integer, file ->
def files = channel.putIfAbsent(integer, [])
files.addAll([file1, file2])
}

[:]创建空映射,而[]创建空列表。

最新更新