Reputation: 590
I am using JDK8 streams in my project .
From the third party application i am going to get the soap xml response mentioned below . According to requirement i have to capture the MetaDataItem which contains the ExecutionCode and the corresponding Value .
<Response>
<ProcessingResults>
<Identifier>identifier-A</Identifier>
<StepResult>
<Identifier>device</Identifier>
<DataItem>
<Identifier>key</Identifier>
<MetaData>
<MetaDataItem>
<Name>ExecutionCode</Name>
<Value>0</Value>
</MetaDataItem>
</MetaData>
</DataItem>
</StepResult>
</ProcessingResults>
</Response>
I am not sure how can i traverse the object till MetaDataItem using JDK8 Streams. I tried but no luck for me
In my JAXB classes
1. Response class contains "List<ProcessingResults>".
2. Each ProcessingResult class contains the "List<StepResults>".
3. StepResult class contains the "List<DataIteam>".
4. DataItem class contains the "List<MetaDataItem>".
Upvotes: 3
Views: 107
Reputation: 7269
Looks like you need Stream.flatMap
. Try this:
List<MetaDataItem> list=
response.getProcessingResult().stream()
.map(ProcessingResult::getStepResults).flatMap(Collection::stream)
.map(StepResult::getDataItms).flatMap(Collection::stream)
.map(DataItem::getMetaDataItems).flatMap(Collection::stream)
.collect(Collectors.toList());
Upvotes: 2
Reputation: 21124
All you have to do is apply sequence of flatMap
operators to get what you need. Here's how it looks,
List<MetaDataItem> metaDataItms = response.getProcessingResult().stream()
.flatMap(pr -> pr.getStepResults().stream())
.flatMap(sr -> sr.getDataItms().stream())
.flatMap(ditm -> ditm.getMetaDataItems().stream())
.collect(Collectors.toList());
Upvotes: 2