0

我有如下方法:

  public void method ()
  {
        List<String> list1 = someOperation();
        List<List2Class> list2;
        long failedCount = 0;
        for (String element : list1) {
            try {
                list2 = someclass.method1(element);
                list2 = someclass.method2(element);
                someclass.saveToDB(list2);
            
            } catch (Exception e) {
                failedCount++;
              
            }
        }
       //some checks on failedCount
    }

我想将 for 循环转换为并行流,有人可以告诉我上述方法中的代码更改应该是什么吗?PS - method1 和 method2 正在返回 list2 的修改版本。

4

1 回答 1

0

这里的逻辑基本上是“最后一次成功操作的结果”。

假设您不需要failedCount(您没有显示它正在使用),您可以这样做:将成功的操作映射到 present Optional,将失败的操作映射到缺席Optional; 并且只取最后一个可选的:

Optional<List<List2Class>> opt = list1.stream()
    .flatMap(element -> Stream.of(runOperation(someclass::method1, element), runOperation(someclass::method2, element))
    .reduce((a, b) -> !b.isPresent() ? a : b);

runOperation类似的东西在哪里:

Optional<List<List2Class>> runOperation(Function<String, List<List2Class>> operation, String parameter) {
  try {
    return Optional.of(operation.apply(parameter));
  } catch (Exception e) {
    return Optional.absent();
  }
}

然后,您需要决定list2如果没有操作成功,应该有什么值。

如果您确实需要failedCount,您可以将其分解一下:

Stream<Optional<List<List2Class>>> intermediate =
    list1.stream()
        .flatMap(element -> Stream.of(runOperation(someclass::method1, element), runOperation(someclass::method2, element));

Map<Boolean, List<Optional<List<List2Class>>>> partitioned =
    intermediate.collect(Collectors.partitioningBy(Optional::isPresent));

现在partitioned.get(true)拥有所有成功的结果,同时partitioned.get(false)拥有所有失败的结果。所以:

Optional<List<List2Class>> opt = partitioned.get(true).stream().reduce((a, b) -> !b.isPresent() ? a : b);
long failedCount = partitioned.get(false).size();
于 2021-08-17T09:27:27.873 回答