如何从这些对象的列表中获取对象拥有的映射作为属性
我有一个 BOLReference
对象,如下所示:
private String ediTransmissionId;
private List<WorkflowExceptions> workflowExceptions;
内部 WorkflowExceptions
如下所示:
private String category;
private Map<String,String> businessKeyValues;
我想获取特定的 Map
<基于某些过滤器的 WorkflowExceptions
的 列表 中的 em>businessKeyValues。我怎样才能这样做呢?
Map<String,String> bKeyMap = bolRef.get(0).getWorkflowExceptions()
.stream().filter(wk->wk.getBusinessKeyValues().containsKey("ABC123"));
I have an BOLReference
object as follows:
private String ediTransmissionId;
private List<WorkflowExceptions> workflowExceptions;
And the inner WorkflowExceptions
looks like below:
private String category;
private Map<String,String> businessKeyValues;
I want to get a particular Map<String,String>
businessKeyValues from the list of WorkflowExceptions
based on some filters. How can I do so?
Map<String,String> bKeyMap = bolRef.get(0).getWorkflowExceptions()
.stream().filter(wk->wk.getBusinessKeyValues().containsKey("ABC123"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了获取包含某个key的map
businessKeyValues
,首先需要应用map()
操作从WorkflowExceptions
对象中提取map。然后应用
filter()
操作,就像您在代码中所做的那样。findFirst()
(返回流中第一个遇到的元素)作为终端操作。方法
findFirst()
返回一个可选对象,因为结果可能存在于流中,也可能不存在。可选
类为您提供了多种方法,允许根据您的需要以不同的方式处理结果不存在的情况。下面我使用了orElse()
方法,如果未找到结果,它将提供一个空地图。您可能会考虑的其他选项:
orElseThrow()
、orElseGet()
、or()
(与其他方法结合使用)。In order to obtain the map
businessKeyValues
that contains a certain key, first, you need to applymap()
operation to extract the map from aWorkflowExceptions
object.Then apply
filter()
operation as you've done in your code. AndfindFirst()
(which returns the first encountered element in the stream) as a terminal operation.Method
findFirst()
returns an optional object, because the result may or may not be present in the stream.Optional
class offers you a wide variety of methods that allow to treat the situation when result is not present in different ways depending on your needs. Down below I've usedorElse()
method that'll provide an empty map if result wasn't found.Other options you might consider:
orElseThrow()
,orElseGet()
,or()
(in conjunction with other methods).