jdk8 优化以下代码: for (DqExecuteResult dqExecuteResult : dqExecuteResults) { String errorOutputPath = dqExecuteResult.getErrorOutputPath(); Path path = new Path(errorOutputPath); R<List<String>> getFileResult = resourceClient.getFilesAtPath(path.toUri().getPath()); if (null != getFileResult && getFileResult.isSuccess()) { for (String fullPath : getFileResult.getData()) { R<List<String>> previewResult = resourceClient.viewFileByPath(new Path(fullPath).toUri().getPath(), query.getCurrent(), query.getSize(), "\n"); if (null != previewResult && previewResult.isSuccess()) { for (MetaColumn tableColumn : getTableColumns(datasourceName, tableName)) { headers.add(tableColumn.getColumnName()); } List<String> datas = previewResult.getData(); contents = processCSVData(datas, headers); } } } }
时间: 2024-02-27 11:53:02 浏览: 139
jdk8新特性代码
可以考虑使用并行流来优化代码,如下所示:
```
dqExecuteResults.parallelStream()
.map(DqExecuteResult::getErrorOutputPath)
.map(Path::new)
.map(Path::toUri)
.map(URI::getPath)
.map(resourceClient::getFilesAtPath)
.filter(Objects::nonNull)
.filter(R::isSuccess)
.flatMap(r -> r.getData().stream())
.map(Path::new)
.map(Path::toUri)
.map(URI::getPath)
.map(fullPath -> resourceClient.viewFileByPath(fullPath, query.getCurrent(), query.getSize(), "\n"))
.filter(Objects::nonNull)
.filter(R::isSuccess)
.flatMap(r -> {
List<MetaColumn> tableColumns = getTableColumns(datasourceName, tableName);
headers.addAll(tableColumns.stream().map(MetaColumn::getColumnName).collect(Collectors.toList()));
List<String> datas = r.getData();
return processCSVData(datas, tableColumns).stream();
})
.forEach(contents::add);
```
这里使用了并行流来提高代码的执行效率,同时使用了 lambda 表达式和方法引用来简化代码。注意,这里的 `headers` 和 `contents` 都需要是线程安全的。
阅读全文