Groovy 迭代和更新 Map<String,List<String>> 值
Groovy iterating and updating Map<String,List<String>> values
我有一个Map<String,List<String>> invoiceErrorLines
如下
invoiceErrorLines = ['1660277':['Line : 1 Invoice does not foot Reported', 'Line : 1 MATH ERROR'],
'1660278':['Line : 5 Invoice does not foot Reported'],
'1660279':['Line : 7 Invoice does not foot Reported'],
'1660280':['Line : 9 Invoice does not foot Reported']]
正在遍历地图并更改错误消息的行号,如下所示,但在打印 invoiceErrorLines
地图时没有看到更新的错误消息
invoiceErrorLines.each{ invNum ->
invNum.value.each{
int actualLineNumber = getActualLineNumber(it)
it.replaceFirst("\d+", String.valueOf(actualLineNumber))
}
}
有人可以帮我解决这个问题吗?
您只是迭代字符串并对它们调用 replaceFirst
。这不会更改您的数据。您宁愿 collect
那里有您的数据。例如:
invoiceErrorLines = [
'1660277':['Line : 1 Invoice does not foot Reported', 'Line : 1 MATH ERROR'],
'1660278':['Line : 5 Invoice does not foot Reported'],
'1660279':['Line : 7 Invoice does not foot Reported'],
'1660280':['Line : 9 Invoice does not foot Reported']
]
println invoiceErrorLines.collectEntries{ k,v ->
[k, v.collect{ it.replaceFirst(/\d+/, '1') }]
}
// Results: =>
[
1660277: [Line : 1 Invoice does not foot Reported, Line : 1 MATH ERROR],
1660278: [Line : 1 Invoice does not foot Reported],
1660279: [Line : 1 Invoice does not foot Reported],
1660280: [Line : 1 Invoice does not foot Reported]
]
我有一个Map<String,List<String>> invoiceErrorLines
如下
invoiceErrorLines = ['1660277':['Line : 1 Invoice does not foot Reported', 'Line : 1 MATH ERROR'],
'1660278':['Line : 5 Invoice does not foot Reported'],
'1660279':['Line : 7 Invoice does not foot Reported'],
'1660280':['Line : 9 Invoice does not foot Reported']]
正在遍历地图并更改错误消息的行号,如下所示,但在打印 invoiceErrorLines
地图时没有看到更新的错误消息
invoiceErrorLines.each{ invNum ->
invNum.value.each{
int actualLineNumber = getActualLineNumber(it)
it.replaceFirst("\d+", String.valueOf(actualLineNumber))
}
}
有人可以帮我解决这个问题吗?
您只是迭代字符串并对它们调用 replaceFirst
。这不会更改您的数据。您宁愿 collect
那里有您的数据。例如:
invoiceErrorLines = [
'1660277':['Line : 1 Invoice does not foot Reported', 'Line : 1 MATH ERROR'],
'1660278':['Line : 5 Invoice does not foot Reported'],
'1660279':['Line : 7 Invoice does not foot Reported'],
'1660280':['Line : 9 Invoice does not foot Reported']
]
println invoiceErrorLines.collectEntries{ k,v ->
[k, v.collect{ it.replaceFirst(/\d+/, '1') }]
}
// Results: =>
[
1660277: [Line : 1 Invoice does not foot Reported, Line : 1 MATH ERROR],
1660278: [Line : 1 Invoice does not foot Reported],
1660279: [Line : 1 Invoice does not foot Reported],
1660280: [Line : 1 Invoice does not foot Reported]
]