List<String> titleList = list1.stream().map(HomeworkVO::getTitle).collect(Collectors.toList());
时间: 2024-06-07 11:07:42 浏览: 12
这段代码使用 Java 8 中的 Stream API 对 `list1` 集合中的 `HomeworkVO` 对象进行处理,将每个对象的 `title` 属性提取出来,然后将这些 `title` 属性组成一个新的集合并返回。具体解释如下:
1. `list1.stream()` 将 `list1` 集合转换成一个 Stream 对象,以便进行后续的处理。
2. `map(HomeworkVO::getTitle)` 对 Stream 中的每个元素,调用 `HomeworkVO` 对象的 `getTitle()` 方法,将其转换成一个新的 Stream,其中每个元素是 `HomeworkVO` 对象的 `title` 属性。
3. `collect(Collectors.toList())` 将 Stream 中的元素收集到一个 List 集合中,返回结果。
相关问题
帮我优化这段代码 private Map<String, Object> boTOMap(DownloadEpidHfmCaseSevereDeathTableVO tableVO){ this.setSonList(tableVO); List<Map<String, Object>> listMap = CodeToStringUtil.dataObjectProcessing(this.getDataObjectProcessingUtilBO(Arrays.asList(tableVO), true, chinesePattern, kg)); if(CollectionUtils.isNotEmpty(listMap)){ Map<String, Object> objectMap = listMap.get(0); //疫苗 List<EpidHfmCaseSevereDeathTestTableVO> selfTestTableVOS = tableVO.getTestTableVOS().stream().filter(item -> item.getJcdx() == DetectionObjectEnum.SELF).collect(Collectors.toList()); List<EpidHfmCaseSevereDeathTestTableVO> familyMembersTestTableVOS = tableVO.getTestTableVOS().stream().filter(item -> item.getJcdx() == DetectionObjectEnum.FAMILY_MEMBERS).collect(Collectors.toList()); if(CollectionUtils.isNotEmpty(selfTestTableVOS)){ objectMap.put("selfTestTable", this.testTableToMap(selfTestTableVOS)); } if(CollectionUtils.isNotEmpty(familyMembersTestTableVOS)){ objectMap.put("familyMembersTestTable", this.testTableToMap(familyMembersTestTableVOS)); } if(CollectionUtils.isNotEmpty(tableVO.getVaccinateTableVOS())){ objectMap.put("vaccinateTable", this.vaccinateTableToMap(tableVO.getVaccinateTableVOS())); } List<Map<String, Object>> dateList = epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableDateList1(tableVO.getId()); dateList.addAll(epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableDateList2(tableVO.getId(), 7-dateList.size())); if(dateList.size() < 7){ int size = dateList.size(); for (int i = 0; i < (7-size); i++) { Map<String, Object> m = new HashMap<>(); m.put("date", "-"); dateList.add(m); } } objectMap.put("titleList", dateList); if(CollectionUtils.isNotEmpty(tableVO.getComplicationTableVOS())){ List<Map<String, Object>> complicationList = null; Map<ComplicationTypeEnum, List<EpidHfmCaseSevereDeathComplicationTableVO>> listMap1 = tableVO.getComplicationTableVOS().stream().collect(Collectors.groupingBy(EpidHfmCaseSevereDeathComplicationTableVO::getType)); int num = 0; for (int j = 0; j < ComplicationTypeEnum.values().length; j++) { complicationList = new ArrayList<>(); List<Map<String, Object>> bfzmcList = epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableBfzmcList( ComplicationTypeEnum.values()[j], tableVO.getId()); for (Map<String, Object> bfzmc : bfzmcList) { Map<String, Object> complication = new HashMap<>(); complication.put("bfzmc7", bfzmc.get("bfzmc")); for (int i = 0; i < dateList.size(); i++) { num = 0; for (EpidHfmCaseSevereDeathComplicationTableVO complicationTableVO : listMap1.get(ComplicationTypeEnum.values()[j])) { if(complicationTableVO.getDate().equals(dateList.get(i).get("title")) && complicationTableVO.getBfzmc().equals(bfzmc.get("bfzmc"))){ complication.put("bfzmc"+i, StringUtils.isNotBlank(complicationTableVO.getJg()) ? complicationTableVO.getJg() : ""); num = 1; break; } } if(num == 0){ complication.put("bfzmc"+i, ""); } } complicationList.add(complication); } objectMap.put("complicationType"+j, complicationList); } } return objectMap; } return null; }
首先,这段代码缺少注释,不容易理解代码的作用。建议加入注释,增加可读性。
其次,可以将一些重复的代码抽取成方法,提高代码复用性和可维护性。例如:
1. 抽取出获取测试表和疫苗表的方法:
```
private void addTestTableAndVaccinateTable(Map<String, Object> objectMap, DownloadEpidHfmCaseSevereDeathTableVO tableVO) {
List<EpidHfmCaseSevereDeathTestTableVO> selfTestTableVOS = tableVO.getTestTableVOS().stream().filter(item -> item.getJcdx() == DetectionObjectEnum.SELF).collect(Collectors.toList());
List<EpidHfmCaseSevereDeathTestTableVO> familyMembersTestTableVOS = tableVO.getTestTableVOS().stream().filter(item -> item.getJcdx() == DetectionObjectEnum.FAMILY_MEMBERS).collect(Collectors.toList());
if(CollectionUtils.isNotEmpty(selfTestTableVOS)){
objectMap.put("selfTestTable", this.testTableToMap(selfTestTableVOS));
}
if(CollectionUtils.isNotEmpty(familyMembersTestTableVOS)){
objectMap.put("familyMembersTestTable", this.testTableToMap(familyMembersTestTableVOS));
}
if(CollectionUtils.isNotEmpty(tableVO.getVaccinateTableVOS())){
objectMap.put("vaccinateTable", this.vaccinateTableToMap(tableVO.getVaccinateTableVOS()));
}
}
private Map<String, Object> testTableToMap(List<EpidHfmCaseSevereDeathTestTableVO> testTableVOS) {
// TODO: 实现将测试表转为 Map 的逻辑
}
private Map<String, Object> vaccinateTableToMap(List<EpidHfmCaseSevereDeathVaccinateTableVO> vaccinateTableVOS) {
// TODO: 实现将疫苗表转为 Map 的逻辑
}
```
2. 抽取出获取并补全并发症表格的方法:
```
private void addComplicationTable(Map<String, Object> objectMap, DownloadEpidHfmCaseSevereDeathTableVO tableVO, List<Map<String, Object>> dateList) {
if(CollectionUtils.isNotEmpty(tableVO.getComplicationTableVOS())){
List<Map<String, Object>> complicationList = null;
Map<ComplicationTypeEnum, List<EpidHfmCaseSevereDeathComplicationTableVO>> listMap1 = tableVO.getComplicationTableVOS().stream().collect(Collectors.groupingBy(EpidHfmCaseSevereDeathComplicationTableVO::getType));
int num = 0;
for (int j = 0; j < ComplicationTypeEnum.values().length; j++) {
complicationList = new ArrayList<>();
List<Map<String, Object>> bfzmcList = epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableBfzmcList(ComplicationTypeEnum.values()[j], tableVO.getId());
for (Map<String, Object> bfzmc : bfzmcList) {
Map<String, Object> complication = new HashMap<>();
complication.put("bfzmc7", bfzmc.get("bfzmc"));
for (int i = 0; i < dateList.size(); i++) {
num = 0;
for (EpidHfmCaseSevereDeathComplicationTableVO complicationTableVO : listMap1.get(ComplicationTypeEnum.values()[j])) {
if(complicationTableVO.getDate().equals(dateList.get(i).get("title")) && complicationTableVO.getBfzmc().equals(bfzmc.get("bfzmc"))){
complication.put("bfzmc"+i, StringUtils.isNotBlank(complicationTableVO.getJg()) ? complicationTableVO.getJg() : "");
num = 1;
break;
}
}
if(num == 0){
complication.put("bfzmc"+i, "");
}
}
complicationList.add(complication);
}
objectMap.put("complicationType"+j, complicationList);
}
}
}
```
这样,原来的代码可以简化为:
```
private Map<String, Object> boTOMap(DownloadEpidHfmCaseSevereDeathTableVO tableVO) {
this.setSonList(tableVO);
List<Map<String, Object>> listMap = CodeToStringUtil.dataObjectProcessing(this.getDataObjectProcessingUtilBO(Arrays.asList(tableVO), true, chinesePattern, kg));
if(CollectionUtils.isNotEmpty(listMap)){
Map<String, Object> objectMap = listMap.get(0);
addTestTableAndVaccinateTable(objectMap, tableVO);
List<Map<String, Object>> dateList = epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableDateList1(tableVO.getId());
dateList.addAll(epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableDateList2(tableVO.getId(), 7-dateList.size()));
if(dateList.size() < 7){
int size = dateList.size();
for (int i = 0; i < (7-size); i++) {
Map<String, Object> m = new HashMap<>();
m.put("date", "-");
dateList.add(m);
}
}
objectMap.put("titleList", dateList);
addComplicationTable(objectMap, tableVO, dateList);
return objectMap;
}
return null;
}
```
怎样将sheet2的表头也添加为titleList。List<String> titleList = new ArrayList<>(); titleList.add("预备字段"); titleList.add("预备字段"); excel.sheet().sheetName("服务器网络连通刻画"); excel.row(0, 0).value(titleList.toArray()); // 添加第二个sheet excel.sheet(1).sheetName("存储介质使用");
要将Sheet2的表头也添加为titleList,可以按照以下步骤操作:
1. 获取Sheet2的表头(即第一行数据)。
2. 将表头数据添加到titleList中。
3. 在添加第二个sheet的代码中,也将titleList作为表头添加到第二个sheet中。
具体的代码实现如下:
```
// 获取Sheet2的表头
Row headerRow = excel.sheet(1).row(0);
List<String> headerList = new ArrayList<>();
for (Cell cell : headerRow) {
headerList.add(cell.getStringCellValue());
}
// 将表头数据添加到titleList中
List<String> titleList = new ArrayList<>();
titleList.addAll(headerList);
// 添加第二个sheet,并将titleList作为表头添加到第二个sheet中
excel.sheet(1).sheetName("存储介质使用");
excel.row(0, 1).value(titleList.toArray());
```
注意,上述代码中的excel对象是指一个Excel文件,需要根据具体情况进行初始化。同时,代码中的行号和列号是从0开始的。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)