jdk1.8 stream() 把List <String>变成String

jdk1.8 stream() 把List 变成String

List<String> zts = ss.getZts();
if (!CollectionUtils.isEmpty(zts)){
	String zt = zts.stream().collect(Collectors.joining("','", "'", "'"));
}
就可以变成'1','2' 这种形式
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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()) { if("原始库".equals(datasourceName)){ Long datasourceId = iDataSourceClient.queryRawDataSource().getId(); List<MetaColumn> tableColumns = iDataSourceMetaClient.getTableColumns(datasourceId, tableName); for (MetaColumn metaColumn : tableColumns){ headers.add(metaColumn.getColumnName()); } } else if("标准库".equals(datasourceName)){ Long datasourceId = iDataSourceClient.queryModelDataSource().getId(); List<MetaColumn> tableColumns = iDataSourceMetaClient.getTableColumns(datasourceId, tableName); for (MetaColumn metaColumn : tableColumns){ headers.add(metaColumn.getColumnName()); } } int headerSize = headers.size(); List<String> datas = previewResult.getData(); StringBuilder contextBuilder = new StringBuilder(); for(String data : datas){ contextBuilder.append(data.replaceAll("[\r\n\t]", " ")); contextBuilder.append("\n"); } StringReader reader = new StringReader(contextBuilder.toString()); Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(reader); for (CSVRecord record : records) { if (record.size() == headerSize){ List<String> content = new ArrayList<>(); for (String column : record) { content.add(column); } contents.add(content); } } } } } }
06-02
可以通过以下方式对代码进行优化: 1. 首先,可以将重复的代码段提取为一个方法,以便在两个 if 块中重用。例如: ``` private List<MetaColumn> getTableColumns(String datasourceName, String tableName) { Long datasourceId; if ("原始库".equals(datasourceName)) { datasourceId = iDataSourceClient.queryRawDataSource().getId(); } else if ("标准库".equals(datasourceName)) { datasourceId = iDataSourceClient.queryModelDataSource().getId(); } return iDataSourceMetaClient.getTableColumns(datasourceId, tableName); } ``` 2. 其次,可以将 CSV 解析和数据处理逻辑提取为一个方法: ``` private List<List<String>> processCSVData(List<String> datas, List<String> headers) throws IOException { int headerSize = headers.size(); List<List<String>> contents = new ArrayList<>(); StringBuilder contextBuilder = new StringBuilder(); for (String data : datas) { contextBuilder.append(data.replaceAll("[\r\n\t]", " ")); contextBuilder.append("\n"); } StringReader reader = new StringReader(contextBuilder.toString()); Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(reader); for (CSVRecord record : records) { if (record.size() == headerSize) { List<String> content = new ArrayList<>(); for (String column : record) { content.add(column); } contents.add(content); } } return contents; } ``` 3. 然后,可以使用 Java 8 的 Lambda 表达式和方法引用来简化代码。例如: ``` dqExecuteResults.stream() .map(DqExecuteResult::getErrorOutputPath) .map(Path::new) .map(Path::toUri) .map(URI::getPath) .map(resourceClient::getFilesAtPath) .filter(Objects::nonNull) .filter(R::isSuccess) .flatMap(result -> result.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(result -> { List<String> datas = result.getData(); List<String> headers = new ArrayList<>(); if ("原始库".equals(datasourceName) || "标准库".equals(datasourceName)) { getTableColumns(datasourceName, tableName).stream() .map(MetaColumn::getColumnName) .forEach(headers::add); } try { return processCSVData(datas, headers).stream(); } catch (IOException e) { e.printStackTrace(); return Stream.empty(); } }) .forEach(contents::add); ``` 这段代码使用了 stream 操作来简化循环和条件语句,使用方法引用、Lambda 表达式和函数式接口来写出更简洁的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值