在groovy中创建一个列表,其中只包含给定Map列表中某些键的更新值

trnvg8h3  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(135)

我有这张Map的清单:

[
[a:1998-01-14, b:2028-11-05, c:OQSeMPIcHNP, d:ASD, e:DEF, f:UuzSJiVvxjhJipzxUPsSsbmaeMvOT, g:mef, e:P00003036, h:P], 
[a:1998-08-22, b:2028-11-05, c:fDScShsqpKqreHPqpANUrnZklN, d:LAS, e:FGH, f:lKuMgRxxrVuwCEXMNiIERHOcUCNmbG, g:ela, e:P00006583, h:P], 
[a:1992-02-29, b:2031-01-01, c:SOThmQNAbKexnvDaxOi, d:MAR, e:ZAD, f:tkYiSUxSoTZrceRoIOYYsZztvvnzkno, g:ela, e:P00002839, h:P]
]

我需要创建一个新的Map列表,或者更新现有的Map列表,方法是将所有键c和f的值都大写。
预期成果:

[
[a:1998-01-14, b:2028-11-05, c:OQSEMPICHNP, d:ASD, e:DEF, f:UUZSJIVVXJHJIPZXUPSSSBMAEMVOT, g:mef, e:P00003036, h:P], 
[a:1998-08-22, b:2028-11-05, c:FDSCSHSQPKQREHPQPANURNZKLN, d:LAS, e:FGH, f:LKUMGRXXRVUWCEXMNIIERHOCUCNMBG, g:ela, e:P00006583, h:P], 
[a:1992-02-29, b:2031-01-01, c:SOTHMQNABKEXNVDAXOI, d:MAR, e:ZAD, f:TKYISUXSOTZRCEROIOYYSZZTVVNZKNO, g:ela, e:P00002839, h:P]
]

如何使用groovy实现这一点?

ryevplcw

ryevplcw1#

请尝试以下操作:

def source = [
    [a:'1998-01-14', b:'2028-11-05', c:'OQSeMPIcHNP', d:'ASD', e:'DEF', f:'UuzSJiVvxjhJipzxUPsSsbmaeMvOT', g:'mef', e1:'P00003036', h:'P'],
    [a:'1998-08-22', b:'2028-11-05', c:'fDScShsqpKqreHPqpANUrnZklN', d:'LAS', e:'FGH', f:'lKuMgRxxrVuwCEXMNiIERHOcUCNmbG', g:'ela', e1:'P00006583', h:'P'],
    [a:'1992-02-29', b:'2031-01-01', c:'SOThmQNAbKexnvDaxOi', d:'MAR', e:'ZAD', f:'tkYiSUxSoTZrceRoIOYYsZztvvnzkno', g:'ela', e1:'P00002839', h:'P']
]

def target = source.collect {
    it.c = it.c?.toUpperCase()
    it.f = it.f?.toUpperCase()
    it
}

println "target = $target"

相关问题