PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

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

 November 04, 2022     collections, java, java-stream, lambda, spring     No comments   

Issue

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


Solution

maybe this will help

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


Answered By - iroli
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing