Consider the following class
public class samplejson {
private String id;
private List<childmain> orders = new ArrayList<>();
// getters & setters
}
After setting values in the class the output is below :
{
"main": {
"id": 1,
"childmain": [
{
"id": 2,
"childmain": [
{
"id": 3,
"childmain": [
]
}
]
},
{
"id": 4,
"childmain": [
{
"id": 5,
"childmain": [
{
"id": 6,
"childmain": [
]
}
]
}
]
}
]
}
}
Question: I want all the id in the child class in a list. I have tried using streams . I got the id values of immediate child. I want the value of the nested child also.
Code I tried:
List<childmain> somethings= samplejson.getchildmain();
List<String> cleanList = somethings.stream().map(somethings::getid)).collect(Collectors.toList());
Expected output: [2,3,4,5,6]
Actual output: [2,4]