How to Update Each Element in a List in Java 8 Using Stream API

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:
.flatMap(i -> i == 0 ? Stream.of(i, 0) : Stream.of(i))

This basically:

  1. flatmaps your integer to a stream of itself if non-zero, and a stream of itself and an additional zero if equal to zero
  2. limits the size of your list to the original size

If this helped, you can accept this answer.

Related Topics

Leave a reply