List<String> list = new ArrayList<String>(); Collections.addAll(list, "a", "b", "c");
时间: 2023-07-19 16:06:01 浏览: 113
这段代码创建了一个 ArrayList 对象,并向其中添加了三个元素,分别为字符串 "a"、"b" 和 "c"。可以通过以下两行代码打印出列表中的元素:
```
for (String str : list) {
System.out.println(str);
}
```
或者使用 Java 8 引入的 Lambda 表达式:
```
list.forEach(System.out::println);
```
这两种方式都能输出如下内容:
```
a
b
c
```
相关问题
ArrayList<String> strings = new ArrayList<>(); ArrayList<Integer> integers = new ArrayList<>(); Collections.addAll(strings,"asdds","sd","a"); Collections.addAll(integers,1,5,9,22); Stream<Object> concat = Stream.concat(strings, integers);
这段代码中的 `Stream.concat()` 方法会把两个 `ArrayList` 合并成一个 `Stream`,但由于 `strings` 和 `integers` 的类型不同,所以在合并时需要将它们的元素类型转为共同的父类类型 `Object`。因此,`concat` 的类型是 `Stream<Object>`。
需要注意的是,`concat()` 方法返回的是一个新的 `Stream`,而不是修改原有的 `ArrayList`。如果需要将合并后的元素保存到一个新的 `List` 中,可以使用 `collect()` 方法,如下所示:
```
List<Object> list = concat.collect(Collectors.toList());
```
优化这段代码public static void readExcel(String fileName) { List<Version> versions = EasyExcel.read(fileName, Version.class, null).sheet("版本号").doReadSync(); List<TableInfo> tableInfoList = EasyExcel.read(fileName, TableInfo.class, null).sheet("数据库&数据表").doReadSync(); List<TableField> tableFieldList = EasyExcel.read(fileName, TableField.class, null).sheet("字段").doReadSync(); List<HopIndex> hopIndexList = EasyExcel.read(fileName, HopIndex.class, null).sheet("跳数索引").doReadSync(); Map<String, List<TableInfo>> tableInfoMap = tableInfoList.stream().collect(Collectors.groupingBy(TableInfo::getEnDataBaseName)); Map<String, List<TableField>> tableFieldMap = tableFieldList.stream().collect(Collectors.groupingBy(TableField::getEnTableName)); Map<String, List<HopIndex>> hopIndexMap = hopIndexList.stream().collect(Collectors.groupingBy(HopIndex::getEnTableName)); List<TableInfo> tableInfos1 = new ArrayList<>(); List<TableInfo> tableInfos2 = new ArrayList<>(); tableInfoMap.forEach((dbName, tableList) -> { if (tableList.size() < 200) { tableInfos1.addAll(tableList); } else { tableInfos2.addAll(tableList); } }); List<TableField> tableFields = new ArrayList<>(); List<HopIndex> hopIndex = new ArrayList<>(); for (TableInfo tableInfo : tableInfos1) { String enTableName = tableInfo.getEnTableName(); tableFields.addAll(tableFieldMap.get(enTableName)); List<HopIndex> hops = hopIndexMap.get(enTableName); if (hops !=null){ hopIndex.addAll(hops); } } String name = filepath + File.separator + "明细层_V1.0_" + DateUtil.date().toString(DatePattern.PURE_DATE_PATTERN) + "1.xls"; ExcelWriter excelWriter = EasyExcel.write(name).build(); ExcelUtil.writerExcel(excelWriter, "版本号", Version.class, versions); ExcelUtil.writerExcel(excelWriter, "数据库&数据表", TableInfo.class, tableInfos1); ExcelUtil.writerExcel(excelWriter, "字段", TableField.class, tableFields); ExcelUtil.writerExcel(excelWriter, "跳数索引", HopIndex.class, hopIndex); ExcelUtil.close(excelWriter); tableFields.clear(); hopIndex.clear(); for (TableInfo tableInfo : tableInfos2) { String enTableName = tableInfo.getEnTableName(); tableFields.addAll(tableFieldMap.get(enTableName)); List<HopIndex> hops = hopIndexMap.get(enTableName); if (hops !=null){ hopIndex.addAll(hops); } } name = filepath + File.separator + "明细层_V1.0" + DateUtil.date().toString(DatePattern.PURE_DATE_PATTERN) + "_2.xls"; excelWriter = EasyExcel.write(name).build(); ExcelUtil.writerExcel(excelWriter, "版本号", Version.class, versions); ExcelUtil.writerExcel(excelWriter, "数据库&数据表", TableInfo.class, tableInfos2); ExcelUtil.writerExcel(excelWriter, "字段", TableField.class, tableFields); ExcelUtil.writerExcel(excelWriter, "跳数索引", HopIndex.class, hopIndex); ExcelUtil.close(excelWriter); }
这段代码可以进行如下优化:
1. 避免重复读取 Excel 文件。可以将读取 Excel 文件的操作提取出来,作为一个公共方法,然后在需要使用这些数据的地方调用该方法,避免重复读取 Excel 文件。
2. 使用 lambda 表达式和流式操作,简化代码。例如,可以使用 stream().filter() 方法替换原来的 if...else 语句。
3. 避免重复创建对象。例如,可以将 tableFields 和 hopIndex 改为局部变量,避免重复创建。
4. 使用 StringBuilder 拼接字符串,避免重复创建字符串对象。
下面是优化后的代码:
```
public static void readExcel(String fileName) {
List<Version> versions = readExcelSheet(fileName, Version.class, "版本号");
List<TableInfo> tableInfoList = readExcelSheet(fileName, TableInfo.class, "数据库&数据表");
List<TableField> tableFieldList = readExcelSheet(fileName, TableField.class, "字段");
List<HopIndex> hopIndexList = readExcelSheet(fileName, HopIndex.class, "跳数索引");
Map<String, List<TableInfo>> tableInfoMap = tableInfoList.stream()
.collect(Collectors.groupingBy(TableInfo::getEnDataBaseName));
Map<String, List<TableField>> tableFieldMap = tableFieldList.stream()
.collect(Collectors.groupingBy(TableField::getEnTableName));
Map<String, List<HopIndex>> hopIndexMap = hopIndexList.stream()
.collect(Collectors.groupingBy(HopIndex::getEnTableName));
List<TableInfo> tableInfos1 = new ArrayList<>();
List<TableInfo> tableInfos2 = new ArrayList<>();
tableInfoMap.forEach((dbName, tableList) -> {
if (tableList.size() < 200) {
tableInfos1.addAll(tableList);
} else {
tableInfos2.addAll(tableList);
}
});
List<TableField> tableFields;
List<HopIndex> hopIndex;
String name;
ExcelWriter excelWriter;
tableFields = new ArrayList<>();
hopIndex = new ArrayList<>();
for (TableInfo tableInfo : tableInfos1) {
String enTableName = tableInfo.getEnTableName();
tableFields.addAll(tableFieldMap.get(enTableName));
hopIndexMap.getOrDefault(enTableName, Collections.emptyList()).forEach(hopIndex::add);
}
name = new StringBuilder()
.append(filepath)
.append(File.separator)
.append("明细层_V1.0_")
.append(DateUtil.date().toString(DatePattern.PURE_DATE_PATTERN))
.append("1.xls")
.toString();
excelWriter = EasyExcel.write(name).build();
ExcelUtil.writerExcel(excelWriter, "版本号", Version.class, versions);
ExcelUtil.writerExcel(excelWriter, "数据库&数据表", TableInfo.class, tableInfos1);
ExcelUtil.writerExcel(excelWriter, "字段", TableField.class, tableFields);
ExcelUtil.writerExcel(excelWriter, "跳数索引", HopIndex.class, hopIndex);
ExcelUtil.close(excelWriter);
tableFields.clear();
hopIndex.clear();
for (TableInfo tableInfo : tableInfos2) {
String enTableName = tableInfo.getEnTableName();
tableFields.addAll(tableFieldMap.get(enTableName));
hopIndexMap.getOrDefault(enTableName, Collections.emptyList()).forEach(hopIndex::add);
}
name = new StringBuilder()
.append(filepath)
.append(File.separator)
.append("明细层_V1.0")
.append(DateUtil.date().toString(DatePattern.PURE_DATE_PATTERN))
.append("_2.xls")
.toString();
excelWriter = EasyExcel.write(name).build();
ExcelUtil.writerExcel(excelWriter, "版本号", Version.class, versions);
ExcelUtil.writerExcel(excelWriter, "数据库&数据表", TableInfo.class, tableInfos2);
ExcelUtil.writerExcel(excelWriter, "字段", TableField.class, tableFields);
ExcelUtil.writerExcel(excelWriter, "跳数索引", HopIndex.class, hopIndex);
ExcelUtil.close(excelWriter);
}
private static <T> List<T> readExcelSheet(String fileName, Class<T> clazz, String sheetName) {
return EasyExcel.read(fileName, clazz, null)
.sheet(sheetName)
.doReadSync();
}
```
阅读全文