java - 使用另一个 Map <String, List<String>> 中的选择性元素创建一个新的 Map <String, List<String>>

标签 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} 
}

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

当我打印headErrors时我看到与 invoiceErrorLines 相同的 map 但 我期待 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'] 
             ]

有人可以帮我解决这个问题吗?

最佳答案

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]]

关于java - 使用另一个 Map <String, List<String>> 中的选择性元素创建一个新的 Map <String, List<String>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29498625/

相关文章:

java - 为什么我的 servlet 堆栈跟踪为我的类显示 "Unknown Source"?

java - 使用 XCA 配置 Tomcat 8.0 的 SSL

java - Java 中使用的 Heron 公式,但值错误

python - 确定单词列表是否在句子中?

Python正则表达式文件名匹配

javascript - JavaScript 中的数组和集合有什么区别?何时应该选择其中之一?

delphi - 有效地将 delphi/freepascal 集合复制到网格

java - 在命令行中指定抗锯齿属性

ruby - 在 ruby​​ 的网页中查找重复模式

java - Android - 我们在 HashMap<MyEnum, String> 中使用 enum 作为键代替 String 是否合适?