Scenario: There is a situation where I need to set some values to List
of objects based on some field condition using the Java 8 streams API.
Below is the sample of object User
.
public class User{
private int id;
private String name;
private String text;
private boolean isActive;
}
Here is the code I have worked out
List<User> users = userDao.getAllByCompanyId(companyId);
users.stream()
.filter(Objects::nonNull)
.filter(User::isActive)
.peek(user -> user.setText('ABC'))
.filter(user -> !user.isActive())
.peek(user -> user.setText('XYZ')
I know that the way I have written to set the values to object based on condition is wrong.
This is just a try-out using stream, at the end I need to set values to users
object.
Is there any way to handle if-else condition.