我对 Groovy Maps 的问题。我一直在寻找一种以编程方式将新条目添加到 Groovy 映射而不覆盖当前条目的方法。例如
def editsMap = [:]
lineEdits.flag.each
{ lineEdits_Flag ->
editsMap.put('FlagId',lineEdits_Flag.id)
editsMap.put('FlagMnemonic',lineEdits_Flag.mnemonic)
editsMap.put('Action',lineEdits_Flag.action)
println "editsMap: ${editsMap}"
}
第一遍生成这张地图:
editsMap: [FlagId:10001, FlagMnemonic:TRA, Action:review]
但是第二遍覆盖了第一遍:editsMap:[FlagId:10002,FlagMnemonic:REB,Action:deny]
我想要做的是在一张地图中创建多个条目。我需要我的地图来填充这样的东西:
editsMap: [FlagId:10001, FlagMnemonic:TRA, Action:review]
editsMap: [FlagId:10002, FlagMnemonic:REB, Action:deny]
editsMap: [FlagId:10003, FlagMnemonic:UNB, Action:deny]
editsMap: [FlagId:20001, FlagMnemonic:REB, Action:deny]
editsMap: [FlagId:20002, FlagMnemonic:ICD, Action:review]
editsMap: [FlagId:30001, FlagMnemonic:REB, Action:deny]
editsMap: [FlagId:40001, FlagMnemonic:ICD, Action:review]
editsMap: [FlagId:40002, FlagMnemonic:MPR, Action:review]
editsMap: [FlagId:50001, FlagMnemonic:CPT, Action:deny]
editsMap: [FlagId:60001, FlagMnemonic:DTU, Action:deny]
editsMap: [FlagId:70001, FlagMnemonic:ICD, Action:review]
editsMap: [FlagId:70002, FlagMnemonic:MPR, Action:review]
一旦我填充了我的地图,我就需要能够找到某些值来处理消息。我相信我可以使用类似的东西:
def thisValue = appliedEditsMap[FlagId, '10001'] ?: "default"
进行快速查找。
有人可以帮助我了解如何以编程方式将值添加到 Groovy 地图而不覆盖地图中已有的值吗?