Java中迭代Map时如何在内部迭代List对象并为其赋值
public List<ErrorMessage> getErrorMessageDetails(ValidationResult validationResult, String severity) {
Map<List<String>, List<String>> errorObjectsMap = new HashMap<>();
//errorCode= some values in List of strings
//errorMessage= some values in List of strings
errorObjectsMap.put(errorCode, errorMessage);
List<ErrorMessage> errorMessageList = new ArrayList<>();
for (Map.Entry<List<String>, List<String>> pair : errorObjectsMap.entrySet()) {
List<String> errorCodeMsg = pair.getKey() == null ? new ArrayList<>() : pair.getKey();
List<String> errorMsg = pair.getValue() == null ? new ArrayList<>() : pair.getValue();
errorMessageList.add(new ErrorMessage(errorCodeMsg.get(0), errorMsg.get(0)));
// So in above line of code i want to iterate both list k,v and use its keys and values to create list of List<ErrorMessage> errorMessageList dynamically without using index of lists
}
return errorMessageList;
}
请参考上面java方法的建议。
有人可以帮助编写逻辑以在java中以最简洁的方式动态迭代吗?
先感谢您 :)
public List<ErrorMessage> getErrorMessageDetails(ValidationResult validationResult, String severity) {
Map<List<String>, List<String>> errorObjectsMap = new HashMap<>();
//errorCode= some values in List of strings
//errorMessage= some values in List of strings
errorObjectsMap.put(errorCode, errorMessage);
List<ErrorMessage> errorMessageList = new ArrayList<>();
for (Map.Entry<List<String>, List<String>> pair : errorObjectsMap.entrySet()) {
List<String> errorCodeMsg = pair.getKey() == null ? new ArrayList<>() : pair.getKey();
List<String> errorMsg = pair.getValue() == null ? new ArrayList<>() : pair.getValue();
errorMessageList.add(new ErrorMessage(errorCodeMsg.get(0), errorMsg.get(0)));
// So in above line of code i want to iterate both list k,v and use its keys and values to create list of List<ErrorMessage> errorMessageList dynamically without using index of lists
}
return errorMessageList;
}
Please refer above java method to suggestions.
Could someone please help to write logic to iterate dynamically in most concise way in java?
Thank you in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所提出的方法不使用任何输入参数
validationResult
和severity
,并且它显示映射仅包含一个条目。但是,为了简单地构建错误消息列表,可以使用 Stream API 将映射条目流转换为
ErrorMessage
列表,其中映射条目中的列表由提供的索引进行迭代IntStream
和flatMap
用于连接所有条目的错误消息:The presented method does not use any of the input arguments
validationResult
andseverity
and it shows that the map contains only one entry.However, to simply build a list of error messages, Stream API could be used to convert the stream of map entries into the list of
ErrorMessage
where the lists in map entries are iterated by indexes provided byIntStream
andflatMap
is applied to join the error messages from all entries: