/*根据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());
```