result = list.stream().map(RbaseTableDispatch::getTableName).collect(Collectors.toList())解释一下这段代码
时间: 2024-04-26 12:23:54 浏览: 108
这段代码使用了Java8的Stream API来操作一个名为`list`的列表,具体解释如下:
1. `list.stream()`:将列表转换为Stream对象,以便进行后续的Stream操作。
2. `map(RbaseTableDispatch::getTableName)`:对Stream中的每一个元素,都调用其`getTableName()`方法,将返回值映射为一个新的Stream。
3. `collect(Collectors.toList())`:将Stream中的元素收集到List对象中。
综上,这段代码的作用是将列表`list`中每个元素的`tableName`属性提取出来,然后将这些属性值组成一个新的列表返回。其中,`RbaseTableDispatch::getTableName`可以写成`x -> x.getTableName()`的形式,表示对Stream中的每一个元素x都调用`getTableName()`方法。
相关问题
优化以下代码: private List<ErrorOutputFileVo> getErrorDataFileList(Long planId, String datasourceName, Long ruleTemplateId, String columnName, String tableName) { List<ErrorOutputFileVo> errorOutputFiles = new ArrayList<>(); // 按规则过滤taskId List<DataQualityPlanRelationEntity> dataQualityPlanRelationEntities = dataQualityPlanRelationService.relationInfoSearch(planId) .stream().filter(dataQualityPlanRelationEntity -> ruleTemplateId.equals(dataQualityPlanRelationEntity.getRuleId()) && columnName.equalsIgnoreCase(dataQualityPlanRelationEntity.getColumnName()) && tableName.equalsIgnoreCase(dataQualityPlanRelationEntity.getTableName())).collect(Collectors.toList()); if (CollectionUtil.isEmpty(dataQualityPlanRelationEntities)) { return errorOutputFiles; } Long datasourceId = getDatasourceId(datasourceName, dataQualityPlanRelationEntities); List<String> taskIds = dataQualityPlanRelationEntities.stream() .filter(relationInfo -> Objects.equals(datasourceId, relationInfo.getDatasourceId())) .map(DataQualityPlanRelationEntity::getTaskId) .map(String::valueOf).distinct() .collect(Collectors.toList()); // 获取质检结果 List<DqExecuteResult> dqExecuteResults = getDqExecuteResults(taskIds); if (CollectionUtil.isEmpty(dqExecuteResults)) { return errorOutputFiles; } 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 currentPath : getFileResult.getData()) { if(StringUtil.isBlank(currentPath)){ continue; } String fileName = String.format("%s-%s-%s", tableName, columnName, dqExecuteResult.getTaskInstanceId()); String originFileName = new Path(currentPath).getName(); String[] originFileSplit = originFileName.split("-"); if(originFileSplit.length > 2){ fileName += String.format("%s-%s", originFileSplit[0], originFileSplit[1]); } // 将文件信息添加到列表 errorOutputFiles.add( ErrorOutputFileVo .builder() .datasourceId(datasourceId) .tableName(tableName) .fieldName(columnName) .fileName(fileName) .originFileName(originFileName) .taskDefinitionCode(dqExecuteResult.getTaskDefinitionCode()) .taskInstanceId(dqExecuteResult.getTaskInstanceId()).build()); } } } return errorOutputFiles; }
可以将代码优化为:
```
private List<ErrorOutputFileVo> getErrorDataFileList(Long planId, String datasourceName, Long ruleTemplateId, String columnName, String tableName) {
List<ErrorOutputFileVo> errorOutputFiles = new ArrayList<>();
// 按规则过滤taskId
List<DataQualityPlanRelationEntity> dataQualityPlanRelationEntities = dataQualityPlanRelationService.relationInfoSearch(planId)
.stream().filter(dataQualityPlanRelationEntity -> ruleTemplateId.equals(dataQualityPlanRelationEntity.getRuleId()) &&
columnName.equalsIgnoreCase(dataQualityPlanRelationEntity.getColumnName()) &&
tableName.equalsIgnoreCase(dataQualityPlanRelationEntity.getTableName()))
.collect(Collectors.toList());
if (CollectionUtil.isEmpty(dataQualityPlanRelationEntities)) {
return errorOutputFiles;
}
Long datasourceId = getDatasourceId(datasourceName, dataQualityPlanRelationEntities);
List<String> taskIds = dataQualityPlanRelationEntities.stream()
.filter(relationInfo -> Objects.equals(datasourceId, relationInfo.getDatasourceId()))
.map(DataQualityPlanRelationEntity::getTaskId)
.map(String::valueOf)
.distinct()
.collect(Collectors.toList());
// 获取质检结果
List<DqExecuteResult> dqExecuteResults = getDqExecuteResults(taskIds);
if (CollectionUtil.isEmpty(dqExecuteResults)) {
return errorOutputFiles;
}
for (DqExecuteResult dqExecuteResult : dqExecuteResults) {
String errorOutputPath = dqExecuteResult.getErrorOutputPath();
Path path = new Path(errorOutputPath);
R<List<String>> getFileResult = resourceClient.getFilesAtPath(path.toUri().getPath());
if (getFileResult == null || !getFileResult.isSuccess()) {
continue;
}
for (String currentPath : getFileResult.getData()) {
if (StringUtil.isBlank(currentPath)) {
continue;
}
String fileName = String.format("%s-%s-%s", tableName, columnName, dqExecuteResult.getTaskInstanceId());
String originFileName = new Path(currentPath).getName();
String[] originFileSplit = originFileName.split("-");
if (originFileSplit.length > 2) {
fileName += String.format("%s-%s", originFileSplit[0], originFileSplit[1]);
}
// 将文件信息添加到列表
errorOutputFiles.add(
ErrorOutputFileVo.builder()
.datasourceId(datasourceId)
.tableName(tableName)
.fieldName(columnName)
.fileName(fileName)
.originFileName(originFileName)
.taskDefinitionCode(dqExecuteResult.getTaskDefinitionCode())
.taskInstanceId(dqExecuteResult.getTaskInstanceId())
.build());
}
}
return errorOutputFiles;
}
```
优化点如下:
1. 将代码按逻辑块划分,提高可读性。
2. 避免 `null` 引用和空指针异常,增加代码的健壮性。
3. 删除不必要的变量赋值,使代码更加简洁。
4. 使用 `continue` 语句避免嵌套代码块,提高代码可读性。
优化以下代码: public Map<String, List<String>> getUniquenessTableFieldList(Long planId, String datasourceName) { Map<String,List<String>> tableColumnTree = new HashMap<>(); // 按规则和字段过滤taskId List<DataQualityPlanRelationEntity> collect = dataQualityPlanRelationService.relationInfoSearch(planId) .stream() // 过滤出唯一性校验的规则 .filter(dataQualityPlanRelationEntity -> dataQualityPlanRelationEntity.getRuleTemplateId().equals(6L)) .collect(Collectors.toList()); for (DataQualityPlanRelationEntity dataQualityPlanRelationEntity : collect) { if(CollectionUtil.isEmpty(tableColumnTree.get(dataQualityPlanRelationEntity.getTableName()))){ List<String> columnList = new ArrayList<>(); columnList.add(dataQualityPlanRelationEntity.getColumnName()); tableColumnTree.put(dataQualityPlanRelationEntity.getTableName(), columnList); } else { List<String> columnList = tableColumnTree.get(dataQualityPlanRelationEntity.getTableName()); if(columnList.contains(dataQualityPlanRelationEntity.getColumnName())){ columnList.add(dataQualityPlanRelationEntity.getColumnName()); } } } return tableColumnTree; }
可以尝试使用 Java 8 的 Stream API 来优化代码,如下所示:
```
public Map<String, List<String>> getUniquenessTableFieldList(Long planId, String datasourceName) {
List<DataQualityPlanRelationEntity> collect = dataQualityPlanRelationService.relationInfoSearch(planId)
.stream()
.filter(dataQualityPlanRelationEntity -> dataQualityPlanRelationEntity.getRuleTemplateId().equals(6L))
.collect(Collectors.toList());
return collect.stream().collect(Collectors.groupingBy(
DataQualityPlanRelationEntity::getTableName,
Collectors.mapping(DataQualityPlanRelationEntity::getColumnName, Collectors.toList())
));
}
```
这段代码使用了 `groupingBy` 方法来将数据按表名分组,然后使用 `mapping` 方法将每个表名对应的 `DataQualityPlanRelationEntity` 对象的 `columnName` 属性提取出来,最终得到每个表名对应的字段列表。这样就省去了手动循环构建 `Map` 对象的复杂度。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)