如何使用Java Lambda和流对MultipartFile数组进行迭代 [英] How to iterate over MultipartFile array using Java lambda and streams

查看:123
本文介绍了如何使用Java Lambda和流对MultipartFile数组进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码,用于将一些文件附件上传到外部存储.我想知道下面的方法中是否有一种避免循环的方法来遍历MultipartFile []数组,以便使用Java流和lambda函数来完成所有操作.希望有更好的方法实现以下目标

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

public void uploadMyFiles(MultipartFile[] multipartFiles, String path) throws Exception {
    ConcurrentHashMap<String, String> sMap = new ConcurrentHashMap<>();
    ExecutorService myExecutor = Executors.newFixedThreadPool(5);
    for (MultipartFile multipartFile : multipartFiles) {
        CompletableFuture<String> future = CompletableFuture
                .supplyAsync(() -> uploadMyFile(multipartFile, path), myExecutor );
        String status = future.get(10, TimeUnit.SECONDS);
        sMap.put(multipartFile.getOriginalFilename(), status);
    }
}

  private String uploadMyFile(MultipartFile file, String fpath){
    return null;
  }

推荐答案

 private static Map<String, String> retrieveCompletableFuture(
      CompletableFuture<Map<String, String>> futureMap) {
    try {
      return futureMap.get(10, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
      e.printStackTrace();
    }
    return null;
  }

  public void uploadMyFiles(MultipartFile[] multipartFiles) throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(5);

    String s3Path = "/demo-mypath/";

    ExecutorService myExecutor = Executors.newFixedThreadPool(5);
    Arrays.stream(multipartFiles)
        .map(
            multipartFile ->
                CompletableFuture.supplyAsync(() -> uploadMyFile(multipartFile, s3Path), executor))
        .map(cfuture -> retrieveCompletableFuture(cfuture))
        .map(Map::entrySet)
        .collect(Collectors.toMap(Entry::getKey, Entry::getValue));

  }

我已经完全将您的实现实现为lambdas了.请注意,这里future.get()是一个阻塞调用,这意味着它们将按顺序执行(因此不需要ConcurrentHashMap).

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).

如果您正在寻找并行操作,则需要有parallelStream.可以提交任务并等待.在这种情况下,您将需要使用Collectors.toConcurrentMap来收集结果. (请特别注意将流合并为单个Map时可能出现的竞争条件)

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)

这篇关于如何使用Java Lambda和流对MultipartFile数组进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆