2022-09-24

Java stream peek not setting the value to object [duplicate]

I am working with Steam's API, while iterating parent and child objects. I see some issues. How can I fix this?

  parent.getChild().stream().filter(child ->
        child.getStatus().equals("Pending") &&
          child.Date() != null && child.getDate().equals(LocalDate.now())
    ).peek(child -> child.setStatus("Updated"));

Above is not making the status as "Updated", even though the condition matches. But below code works.

parent.getChild().forEach(child -> {
 if (child.getStatus().equals("Pending") &&
      child.Date() != null && child.getDate().equals(LocalDate.now())) {
        child.setStatus("Updated");
      }
 });

Above code with foreach works but not the one with peek. Why is the one with peek not working?



No comments:

Post a Comment