Luis Nox
Luis Nox

Reputation: 3

How to replace null value of object into list without for expression

How i can do this code with stream and lambda functions, in JAVA 8:

//Replace null values with "NO"
for (Product product:
             listProduct) {
            if(product.getIsBreakStock().equals(null)) product.setIsBreakStock("NO");
}

I try with replaceAll function tutorial and foreach(), but IDE throw me an error:

listProduct.forEach(p -> 
                p.getIsBreakStock().equals(null) ? p.setIsBreakStock("NO") : p);

Required type: void Provided: Product

Upvotes: 0

Views: 786

Answers (1)

iroli
iroli

Reputation: 458

maybe this will help

    listProduct.stream().filter(p -> p.getIsBreakStock() == null).peek(p ->  p.setIsBreakStock("NO") ).collect(Collectors.toList());

Upvotes: 1

Related Questions