2015-04-06 93 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

相關問題