I have a nested for loop. Every message class have relation to MessageDetail class (one to many relation) and I want to convert below for loop to stream.
for (Message m:messages){
System.out.println(m.getText());
for (MessageDetail m2:m.getMessageDetails()){
System.out.println(m2.getId+m2.getStatus());
}
}
I try this code but i can not show e.getText in upper level.
messages.stream().flatMap(e-> e.getMessageDetails().stream().map(
e2->e.getText()+"-->{"+e2.getId()+"," +e2.getStatus()+"}"
)).collect(Collectors.toList());
The output like:
[
Hi-->{1,OK},
Hi-->{2,OK},
Hi-->{4,NOT_DELIVER},
Bye-->{5,OK},
Bye-->{6,NOT_DELIVER},
Bye-->{7,NOT_DELIVER},
]
But I want to like this:
[
Hi [ {2,OK},{3,OK},{4,NOT_DELIVER}]
Bye[ {5,OK},{6,NOT_DELIVER},{7,NOT_DELIVER}]
]
messages.stream().flatMap(e-> { System.out.println(e.getText()); return e.getMessageDetails().stream();}).map( e->e.getStatus()+e.getMessage()).forEach(System.out::println);