创建一个新的Map <string,list <string =“”>&gt;使用来自另一个Map <string,list <string =“”>&gt; </string,> </string,>的选择性元素

时间:2015-04-07 18:29:15

标签: java regex collections groovy

尝试使用其他Map <String, List<String>> headErrors

中的选择性元素创建新的Map <String, List<String>> invoiceErrorLines
invoiceErrorLines = ['1660277':['Line : 1 Invoice does not foot Reported', 'Line : 2 MATH ERROR'], 
                    '1660278':['Line : 5 Invoice does not foot Reported', 'cl_id is a required field'], 
                    '1660279':['Line : 7 could not parse date ', 'File Error : The file doesnt have delimiter'], 
                    '1660280':['Line : 9 Invoice error']]
def regex = "^Line\\s(?:(\\d+)\\s)?\\s*:\\s+(\\d+)?.+"
def headErrors = invoiceErrorLines.each{ inv ->
   inv.value.findAll{it.contains('Invoice does not foot Reported') || !(it ==~ regex) }.groupBy{inv.key} 
}

新地图应包含发票编号作为密钥及其相应的错误消息,该消息与regex = "^Line\\s(?:(\\d+)\\s)?\\s*:\\s+(\\d+)?.+"不匹配,但包含Invoice does not foot Reported

当我打印headErrors我看到与invoiceErrorLines相同的地图时,但是 我希望headErrors如下所示

headErrors = ['1660277':['Line : 1 Invoice does not foot Reported'], 
              '1660278':['Line : 5 Invoice does not foot Reported', 'cl_id is a required field'], 
              '1660279':['File Error : The file doesnt have delimiter'] 
             ]

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:3)

使用

def headErrors = invoiceErrorLines.collectEntries{ key, value ->
   value.findAll{ it.contains('Invoice does not foot Reported') || !(it ==~ regex) }.groupBy{ key }
}

它产生

[1660277:[Line : 1 Invoice does not foot Reported], 1660278:[Line : 5 Invoice does not foot Reported, cl_id is a required field], 1660279:[File Error : The file doesnt have delimiter]]