I'm not able to convert below snippet in Java 8 stream format.
List<String> titles = Arrays.asList("First Name", "Last Name");
for (FirstClass first : firstClassList) {
for (SecondClass second : first.getSecondClassList()) {
for (ThirdClass third : second.getThirdClassList()) {
if(!titles.contains(third.getField())) {
second.getThirdClassList().remove(third);
}
}
}
}
I'm comparing third level nested list object against the input list of fields. If fields are not matching then I'm removing them from original list. How can I achieve this using Java 8 syntax.
Edit: I want List of FirstClass to be returned.
firstClassList.stream().flatMap(FirstClass::getSecondClassList()and similar for the second level.