-2

I have the below object, I would like to use Stream() in order to get the first input param where the name is equal to "target" is it possible? or I can only stream the inputParam?

    "flow": [
        {
            "step": [
                {
                    "inputParam": [
                        {
                            "name": "inputname",
                            "value": "value"
                        }
                    ]
                }
            ]
        }
    ]
}```
Shmuel P.
  • 129
  • 1
  • 8
  • 4
    have you tried? If the element a Stream iterates over is a Collection of sorts, why wouldn't you be able to use a Stream? – Stultuske Apr 21 '21 at 07:33

1 Answers1

1

Use flatMap in streams.

more about map and flat map

 Optional<InputParam> optionalInputParam = flowCollection.stream()
                .flatMap(flow -> flow.getSteps().stream())
                .flatMap(step -> step.getInputParams().stream())
                .filter(inputParam -> inputParam.getName().equals("target"))
                .findFirst();

This is assuming you have steps in Flow object and inputParams in Step object and have defined getters on these attributes.

Arun Gowda
  • 2,721
  • 5
  • 29
  • 50