For the following class structure, from a List<SomeClass>, for a given name and fieldName, I want to get a list of fieldValues. There can be multiple occurrences of name
class SomeClass {
String name;
List<SomeField> fields;
}
class SomeField {
String fieldName;
String fieldValue;
}
This is what I have done so far without stream -
for (SomeClass aClass : classes) {
if (aClass.getName().equalsIgnoreCase(givenName)) {
for (SomeField aField : aClass.getSomeField()) {
if (aField.getFieldName().equalsIgnoreCase(givenFieldName)) {
outputList.add(aField.getFieldValue());
}
}
}
}
I have tried to convert this to stream, but I only reached till here. I could not figure out how to proceed to the next step (getting list of SomeField from this point and filtering based on fieldName) -
classes.stream()
.filter(e -> e.getName().equalsIgnoreCase(givenName))
.collect(Collectors.toList());
Any help would be appreciated.
Sample input:
[
{
"name":"a",
"fields":[
{
"fieldName":"n1",
"fieldValue":"v1"
},
{
"fieldName":"n2",
"fieldValue":"v2"
}
]
},
{
"name":"a",
"fields":[
{
"fieldName":"n1",
"fieldValue":"v3"
}
]
},
{
"name":"b",
"fields":[
{
"fieldName":"n1",
"fieldValue":"v4"
}
]
}
]
for givenName = "a" and givenFieldName = "n1" :
expected output : ["v1","v3"]
getSomeField()a getter forList<SomeField> fields?