我有文件和设备的映射,到目前为止它是一对一的映射。
现在我需要实现一对多映射(单个文件到多个设备)
我的一对一映射逻辑是
deviceMapping = new Properties();
params.files.eachWithIndex { file, i ->
def device = devices["${file.key}"]
deviceMapping.put("${device}", "${file.value}");
}
我尝试了不同的方法,但不支持我的一对多映射
- 我尝试循环设备来映射文件
- 尝试将设备的字符串值剪切到映射文件
当前输出
devices :[1.2:SonyEricsson X10i, 1:Samsung I9000 Galaxy, 0:Acer C6, 0.1:HTC Magic]
params files from screen :[0:TheSims3.apk, 1:Tetris.apk]
device mapping :[Acer C6:TheSims3.apk, Samsung I9000 Galaxy:Tetris.apk]
我想要的是:
[Acer C6:TheSims3.apk, Samsung I9000 Galaxy:Tetris.apk, SonyEricsson X10i:Tetris.apk, HTC Magic:TheSims3.apk]
最佳答案
你没有解释你的映射是如何工作的......
但是假设您根据设备版本的第一个字符(?)将设备映射到文件,然后给出:
def devices = [ '1.2': 'SonyEricsson X10i',
'1' : 'Samsung I9000 Galaxy',
'0' : 'Acer C6',
'0.1': 'HTC Magic' ]
def files = [ '0':'TheSims3.apk', '1':'Tetris.apk' ]
你可以这样做:
def mapping = devices.collectEntries { ver, name ->
// Get the first part of the version (up to the first dot)
def v = ver.split( /\./ )[ 0 ]
[ (name): files[ v ] ]
}
或者,如果您使用的是 1.7.9 之前的 Groovy 版本,则可以使用 inject
代替 collectEntries
:
def mapping = devices.inject( [:] ) { map, dev ->
// Get the first part of the version (up to the first dot)
def v = dev.key.split( /\./ )[ 0 ]
map << [ (dev.value): files[ v ] ]
}
给变量映射
,将其设置为:
[ "SonyEricsson X10i":"Tetris.apk",
"Samsung I9000 Galaxy":"Tetris.apk",
"Acer C6":"TheSims3.apk",
"HTC Magic":"TheSims3.apk" ]
关于java - java/groovy 中属性的一对多映射(文件到设备),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8504378/