Skip to content
Advertisement

How to iterate over MultipartFile array using Java lambda and streams

Below is my code to upload some file attachments to external storage. I would like to know if there is a way to avoid for loop in the below method to iterate over the MultipartFile[] array so that everything will be done using the java streams and lambda functions. Would like to have any better way to achieve the below

JavaScript

Advertisement

Answer

JavaScript

I’ve exactly made your implementation to lambdas. Note that here future.get() is a blocking call, which means these execute sequentially (and hence no need for ConcurrentHashMap).

If you are looking for parallel operations, you’ll need to have a parallelStream. which can submit task and wait. In such case, you’ll need to use Collectors.toConcurrentMap to collect the results. (Be very careful of the race conditions that may arise while merging the streams into single Map)

Advertisement