2

Could you please help me here for converting the below forEach into Stream.

Here is my code:

List<Addon> addonItems = <Some Addon List>

for (Addon addOn : addonItems) {
    if (GeoCode.TRP.toString().equals(addonItems.getSomeCode())) {
        addonItems.setName("Got from GOOGLE");
    }
}

1 Answer 1

2

Your example code won't actually compile but i'll attempt to answer your question...

addonItems.stream().filter(addonItem -> GeoCode.TRP.toString().equals(addonItem.getSomeCode()))
    .forEach(addonItem -> addonItem.setName("Got from GOOGLE"));

What this code does is stream the elements in the addonItems list and filter for addonItems based on our lambda filter expression. It will perform this stream operation when the forEach is called and set the name of each addonItem accordingly based on our filter expression.

Sign up to request clarification or add additional context in comments.

1 Comment

addonItems.setName would not compile. 'setName' is not a method available in the list object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.