Updating list of POJOs using Java Stream API
Use forEach
:
orders.forEach(order -> order.setStatus("H"));
Java 8 stream api code to add a element to a List based on condition keeping the list size same
You can achieve this by doing the following:
alist.stream()
.flatMap(i -> i == 0 ? Stream.of(i, 0) : Stream.of(i))
.limit(alist.size())
.collect(Collectors.toList());
This basically:
- flatmaps your integer to a stream of itself if non-zero, and a stream of itself and an additional zero if equal to zero
- limits the size of your list to the original size
If this helped, you can accept this answer.
Related Topics
Java 8 Streams: Multiple Filters Vs. Complex Condition
Warning the Requested Profile "Pom.Xml" Could Not Be Activated Because It Does Not Exist
How to Solve the "Failed to Lazily Initialize a Collection of Role" Hibernate Exception
Bootstrap.Yml Not Loading in Spring Boot 2
Expiry Time @Cacheable Spring Boot
Query in Repository With Limit
Chrome Browser Is Not Opening in Selenium Webdriver
How Does Bufferedreader Read Files from S3
How to Find Index of First Vowel in a String
How to Handle Incorrect User Input into a Scanner
Largest and Smallest Integer - Exercise from Deitel'S Java Book
Reading a Resource File from Within Jar
Selenium Webdriver: Modifying Navigator.Webdriver Flag to Prevent Selenium Detection
Httpclienterrorexception 400 Null Using Resttemplate in Microservices
No Content to Map Due to End-Of-Input Jackson Parser
How to Run Java Program in Terminal With External Library Jar