I want to collect the values returned by a method called inside a forEach:
@PostMapping(value="insert-ppl")
public String insertPeople(@RequestBody @Valid @NotNull List<Person> people){
people.forEach(this::insertPerson);
}
the insertPerson method returns a string signaling whether the insert in the database was successfull or not. I want to take the strings returned from each call to the insertPerson. But I can’t use the .collect because is not a stream.
How can I do that?
>Solution :
The forEach is supposed to be used to execute a consuming operation on each element. It’s not supposed to be used to collect any results.
For this purpose you need to do it differently. I think best option would be to just use a standard for loop and add result of each insertPerson call into a list that you then return at the end.
Theoretically, you could use something like people.stream().map(this::insertPerson).toList() but that’s in my opinion not great idea, because map() is supposed to just map input to output without any side effects, however your insertPerson obviously has side effects, which may make it more "problematic" for a reader to understand what is really happening.