2015-04-06 83 views
0

我有一个Map<String,List<String>> invoiceErrorLines如下Groovy的迭代和更新地图<字符串,列表<String>>值

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)) 
    } 
} 

有人可以帮助我吗?

回答

2

你只是迭代字符串,并在其上调用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] 
] 
+0

我硬编码'1'较早,但它可以是来自任意整数'getActualLineNumber()'请查看更新的问题 – RanPaul

+0

这并不能改变什么吗? – cfrick

+0

不,它不会改变任何东西 – RanPaul

相关问题