Home > Software engineering >  How to replace null value of object into list without for expression
How to replace null value of object into list without for expression

Time:07-07

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

CodePudding user response:

maybe this will help

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