2

How to update a list object where matching condition using java 8

list --> Obj to iterate

    list.stream().filter(d -> { 
        if(d.getAvailableTodayInPerson() == true && d.getDistance() > dis) {
            d.setAvailableTodayInPerson(false);
            d.setAvailableTodayOutPerson(true);
        }
    });
1

2 Answers 2

3

It's simple actually. You can do something like this:

list.stream()
                .filter(d-> d.getAvailableTodayInPerson() == true && d.getDistance() > dis)
                .foreach(d -> {
                    d.setAvailableTodayInPerson(false);
                    d.setAvailableTodayOutPerson(true);
                });
Sign up to request clarification or add additional context in comments.

1 Comment

This changes the original list instead of just applying an update if the condition is matching. I'd remove the collection and assignment.
1

Do you mean?

list.stream().forEach(d -> {
     if(d.getAvailableTodayInPerson() == true && d.getDistance() > dis) {
            d.setAvailableTodayInPerson(false);
            d.setAvailableTodayOutPerson(true);
     }
 });

Comments

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.