/*根据sourceTableId查询数据*/ List<DlLineage> dlLineageList = getLineageListBySourceTableIds(null, null, Arrays.asList(451L)); /*源端数据处理*/ List<DlLineage> dlLineageListCopySource = new ArrayList<>(); Iterator<DlLineage> iterator = dlLineageList.iterator(); while (iterator.hasNext()) { /*获取源端的数据*/ if (iterator.next().getSourceLayer().equals(LayerTypeEnum.SOURCEEND.getCode())) { dlLineageListCopySource.add(iterator.next()); /*删除源端列表数据*/ dlLineageList.remove(iterator.next()); } }产生java.util.ConcurrentModificationException: null异常, if (iterator.next().getSourceLayer().equals(LayerTypeEnum.SOURCEEND.getCode()))出现问题并给出解决办法

时间: 2024-04-10 16:34:53 浏览: 24
产生`java.util.ConcurrentModificationException: null`异常是因为在使用迭代器遍历集合时,修改了集合的结构(删除了元素),导致迭代器失效。 问题出现在以下代码行: ```java dlLineageList.remove(iterator.next()); ``` 解决办法是使用迭代器的 `remove()` 方法来删除元素,而不是直接使用集合的 `remove()` 方法。 以下是修正后的代码示例: ```java Iterator<DlLineage> iterator = dlLineageList.iterator(); while (iterator.hasNext()) { DlLineage dlLineage = iterator.next(); if (dlLineage.getSourceLayer().equals(LayerTypeEnum.SOURCEEND.getCode())) { dlLineageListCopySource.add(dlLineage); iterator.remove(); // 使用迭代器的 remove() 方法删除元素 } } ``` 通过使用迭代器的 `remove()` 方法来删除元素,可以避免 `ConcurrentModificationException` 异常的发生。
相关问题

/*根据sourceTableId查询数据*/ List<DlLineage> dlLineageList = getLineageListBySourceTableIds(null, null, Arrays.asList(451L)); /*源端数据处理*/ List<DlLineage> dlLineageListCopySource = new ArrayList<>(); Iterator<DlLineage> iterator = dlLineageList.iterator(); while (iterator.hasNext()) { /*获取源端的数据*/ if (iterator.next().getSourceLayer().equals(LayerTypeEnum.SOURCEEND.getCode())) { dlLineageListCopySource.add(iterator.next()); /*删除源端列表数据*/ dlLineageList.remove(iterator.next()); } } 将这段代码改编成dlLineageListCopySource 存放LayerTypeEnum.SOURCEEND.getCode())类型的数据,dlLineageList 只存放除了LayerTypeEnum.SOURCEEND.getCode())类型的数据

为了实现将 `dlLineageListCopySource` 存放 `LayerTypeEnum.SOURCEEND.getCode()` 类型的数据,`dlLineageList` 只存放除了 `LayerTypeEnum.SOURCEEND.getCode()` 类型的数据,可以对代码进行如下修改: ```java List<DlLineage> dlLineageList = getLineageListBySourceTableIds(null, null, Arrays.asList(451L)); List<DlLineage> dlLineageListCopySource = new ArrayList<>(); Iterator<DlLineage> iterator = dlLineageList.iterator(); while (iterator.hasNext()) { DlLineage dlLineage = iterator.next(); if (dlLineage.getSourceLayer().equals(LayerTypeEnum.SOURCEEND.getCode())) { dlLineageListCopySource.add(dlLineage); iterator.remove(); // 使用迭代器的 remove() 方法删除元素 } } ``` 这样修改后,`dlLineageListCopySource` 将只包含 `LayerTypeEnum.SOURCEEND.getCode()` 类型的数据,而 `dlLineageList` 将只包含除了 `LayerTypeEnum.SOURCEEND.getCode()` 类型的数据。注意在遍历时,只调用了一次 `iterator.next()` 方法,以避免重复获取元素。

优化以下代码: //获取所有的类目ids List<Long> goodsCategoryIds = new ArrayList<>(); //获取所有的商品ids List<Long> goodsId = new ArrayList<>(); if (!CollectionUtils.isEmpty(buyReturnActivity.getGoodsIds())) { goodsId.addAll(buyReturnActivity.getGoodsIds()); } if (!CollectionUtils.isEmpty(buyReturnActivity.getGoodsCategoryIds())) { goodsCategoryIds.addAll(buyReturnActivity.getGoodsCategoryIds()); } //获取类目的所有ids Set<Long> goodsCategoryId = new HashSet<>(); if (!CollectionUtils.isEmpty(goodsCategoryIds)) { log.info("获取到的多买多返的类目Ids为============" + goodsCategoryIds); Map<Long, CategoryCache> categoryMap2 = this.categoryService.categoryMap(tenantId, goodsCategoryIds); log.info("获取到的类目缓存mapwei ======= " + categoryMap2); goodsCategoryIds.forEach(c -> { if (Objects.isNull(categoryMap2.get(c))) { return; } else { CategoryCache categoryCache1 = categoryMap2.get(c); String[] split = categoryCache1.getIdPaths().split("-"); List<String> strings = Arrays.asList(split); Set<Long> categoryId = strings.stream().map(Long::parseLong).collect(Collectors.toSet()); goodsCategoryId.addAll(categoryId); } }); }

可以优化的地方如下: 1. 可以使用addAll方法替换forEach循环中的if-else条件判断,减少代码量。 2. 可以使用流式编程简化代码逻辑,提高可读性。 重构后的代码如下: ``` List<Long> goodsCategoryIds = new ArrayList<>(); List<Long> goodsId = new ArrayList<>(); goodsId.addAll(buyReturnActivity.getGoodsIds()); goodsCategoryIds.addAll(buyReturnActivity.getGoodsCategoryIds()); Set<Long> goodsCategoryId = goodsCategoryIds.stream() .map(c -> this.categoryService.categoryMap(tenantId, Collections.singletonList(c)).get(c)) .filter(Objects::nonNull) .map(CategoryCache::getIdPaths) .flatMap(s -> Arrays.stream(s.split("-"))) .map(Long::parseLong) .collect(Collectors.toSet()); ```

相关推荐

根据代码1“@Component @Slf4j public class ApplicationServicePlatformClientProxy { @Autowired private IApplicationServicePlatformClient applicationServicePlatformClient; public List<QueryAllProject> listUserProject() { QueryOut> result = null; try { String bspToken = RequestHeaderHolder.getBspToken(); String bspUserId = RequestHeaderHolder.getUserId(); String bspUserEnvId = RequestHeaderHolder.getCompanyId(); String bspUserTenant = RequestHeaderHolder.getCompanyId(); String companyId = RequestHeaderHolder.getCompanyId(); ProjectCondition queryProjectDto = new ProjectCondition(); queryProjectDto.setAppId(queryProjectDto.getAppId()); queryProjectDto.setCompanyId(companyId); queryProjectDto.setDesignStatusList(Arrays.asList(2,3)); queryProjectDto.setPageSize(-1); queryProjectDto.setUserId(bspUserId); result = applicationServicePlatformClient.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, queryProjectDto); log.info("result={}", JSON.toJSONString(result)); } catch (Exception e) { log.error("调用IApplicationServicePlatformClient获取项目名称失败:", e); throw new BimdcException("调用IApplicationServicePlatformClient获取项目名称失败"); } if (result == null || result.getData() == null) { log.error("调用IApplicationServicePlatformClient获取项目名称失败:result={}", result); throw new BimdcException("调用IApplicationServicePlatformClient获取项目名称失败"); } return result.getData().getData(); } }”,修改代码2“public List projectCount(String beginTime, String endTime, Integer forceType) { // 查询任务列表 List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType,null); if (CollectionUtil.isEmpty(taskTaskList)) { return Collections.emptyList(); } List result = new ArrayList<>(); // 将任务按照项目ID分组 Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId)); //查询项目名称 List<QueryAllProject> queryAllProject = applicationServicePlatformClientProxy.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, queryProjectDto);”

换一种方式获取项目名称的代码:@Override public List projectCount(String beginTime, String endTime, Integer forceType, String projectId) { // 查询任务列表 List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId); if (CollectionUtil.isEmpty(taskTaskList)) { return Collections.emptyList(); } List result = new ArrayList<>(); // 将任务按照项目ID分组 Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId)); //查询项目名称 String bspToken = RequestHeaderHolder.getBspToken(); String bspUserId = RequestHeaderHolder.getUserId(); String bspUserEnvId = RequestHeaderHolder.getCompanyId(); String bspUserTenant = RequestHeaderHolder.getCompanyId(); String companyId = RequestHeaderHolder.getCompanyId(); ProjectCondition projectCondition = new ProjectCondition(); projectCondition.setAppId("23031408164321600"); projectCondition.setCompanyId(companyId); projectCondition.setDesignStatusList(Arrays.asList(2,3)); projectCondition.setPageSize(-1); projectCondition.setUserId(bspUserId); QueryAllProject queryAllProject = applicationServicePlatformClientProxy.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, projectCondition); // 遍历项目及对应的任务列表 for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); String projectName = queryAllProject.getName(); projectCountVO.setName(projectName); List<TaskTask> taskList = projectTaskEntry.getValue(); projectCountVO.setTaskCount(taskList.size()); Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream().collect(Collectors.summingInt(task -> Optional.ofNullable(task.getProblemNum()).orElse(0))); projectCountVO.setProblemCount(problemCount); result.add(projectCountVO); } return result; }

根据代码1“public List projectCount(String beginTime, String endTime, Integer forceType, String projectId) { // 查询任务列表 List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId); if (CollectionUtil.isEmpty(taskTaskList)) { return Collections.emptyList(); } List result = new ArrayList<>(); // 将任务按照项目ID分组 Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId)); //查询项目名称 String bspToken = RequestHeaderHolder.getBspToken(); String bspUserId = RequestHeaderHolder.getUserId(); String bspUserEnvId = RequestHeaderHolder.getCompanyId(); String bspUserTenant = RequestHeaderHolder.getCompanyId(); String companyId = RequestHeaderHolder.getCompanyId(); ProjectCondition projectCondition = new ProjectCondition(); projectCondition.setAppId("23031408164321600"); projectCondition.setCompanyId(companyId); projectCondition.setDesignStatusList(Arrays.asList(2,3)); projectCondition.setPageSize(-1); projectCondition.setUserId(bspUserId); List<QueryAllProject> queryAllProject = applicationServicePlatformClientProxy.listUserProject(bspToken, bspUserId, bspUserEnvId, bspUserTenant, projectCondition);”修改代码2“// 遍历项目及对应的任务列表 for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); String projectName = queryAllProject; projectCountVO.setName(projectName); List<TaskTask> taskList = projectTaskEntry.getValue(); projectCountVO.setTaskCount(taskList.size()); Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream().collect(Collectors.summingInt(task -> Optional.ofNullable(task.getProblemNum()).orElse(0))); projectCountVO.setProblemCount(problemCount); result.add(projectCountVO); } return result;”,最终能够查到指定“projectname”

帮我优化这段代码 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; }

最新推荐

recommend-type

grpcio-1.14.0-cp36-cp36m-macosx_10_7_intel.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

哈尔滨工程大学825经济学2020考研专业课初试大纲.pdf

哈尔滨工程大学考研初试大纲
recommend-type

hack-10万密码.txt

hack-10万密码
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB结构体与对象编程:构建面向对象的应用程序,提升代码可维护性和可扩展性

![MATLAB结构体与对象编程:构建面向对象的应用程序,提升代码可维护性和可扩展性](https://picx.zhimg.com/80/v2-8132d9acfebe1c248865e24dc5445720_1440w.webp?source=1def8aca) # 1. MATLAB结构体基础** MATLAB结构体是一种数据结构,用于存储和组织相关数据。它由一系列域组成,每个域都有一个名称和一个值。结构体提供了对数据的灵活访问和管理,使其成为组织和处理复杂数据集的理想选择。 MATLAB中创建结构体非常简单,使用struct函数即可。例如: ```matlab myStruct
recommend-type

详细描述一下STM32F103C8T6怎么与DHT11连接

STM32F103C8T6可以通过单总线协议与DHT11连接。连接步骤如下: 1. 将DHT11的VCC引脚连接到STM32F103C8T6的5V电源引脚; 2. 将DHT11的GND引脚连接到STM32F103C8T6的GND引脚; 3. 将DHT11的DATA引脚连接到STM32F103C8T6的GPIO引脚,可以选择任一GPIO引脚,需要在程序中配置; 4. 在程序中初始化GPIO引脚,将其设为输出模式,并输出高电平,持续至少18ms,以激活DHT11; 5. 将GPIO引脚设为输入模式,等待DHT11响应,DHT11会先输出一个80us的低电平,然后输出一个80us的高电平,
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB结构体与数据库交互:无缝连接数据存储与处理,实现数据管理自动化

![MATLAB结构体与数据库交互:无缝连接数据存储与处理,实现数据管理自动化](https://ww2.mathworks.cn/products/database/_jcr_content/mainParsys/band_1749659463_copy/mainParsys/columns_copy/6d5289a2-72ce-42a8-a475-d130cbebee2e/image_copy_2009912310.adapt.full.medium.jpg/1709291769739.jpg) # 1. MATLAB结构体与数据库交互概述** MATLAB结构体与数据库交互是一种强大的