Skip to content
Advertisement

Get updated object after mapping by lambda expression

I have a list of objects as following,

JavaScript

I want to update one of these objects and I have the following implementation

JavaScript

This returns an object which satisfied with the condition based on filter. Unfortunately this is an not-up-to-date object!

How can I get the updated object after mapping?

Advertisement

Answer

This is because the set method of a List would return the PREVIOUS element at that position and not the newly created one. Refer

Assuming that your id value is unique, I don’t think it is neat to use map and then findFirst even though you could achieve the desired result.

Instead use findFirst and get the element and then then update the details as a second step.

JavaScript

If you still want to do it in single line, then use:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement