List<DlLineage> dlLineageListCopySource = new ArrayList<>(); /*非源端数据*/ Iterator<DlLineage> iterator = dlLineageList.iterator(); /*源端数据处理*/ while (iterator.hasNext()) { /*获取源端的数据*/ DlLineage dlLineage = iterator.next(); if (Optional.ofNullable(dlLineage.getSourceLayer()).equals(LayerTypeEnum.SOURCEEND.getCode())) { dlLineageListCopySource.add(dlLineage); /*删除源端列表数据*/ iterator.remove(); } } 为什么进入到 if (Optional.ofNullable(dlLineage.getSourceLayer()).equals(LayerTypeEnum.SOURCEEND.getCode())) 方法中,dlLineageListCopySource该list不添加长度
时间: 2024-04-21 08:24:46 浏览: 13
根据你提供的代码,进入到`if (Optional.ofNullable(dlLineage.getSourceLayer()).equals(LayerTypeEnum.SOURCEEND.getCode()))`方法中表示`dlLineage`对象的`sourceLayer`属性值等于`LayerTypeEnum.SOURCEEND.getCode()`。
可能的原因是`dlLineage`对象的`sourceLayer`属性值为null或者不等于`LayerTypeEnum.SOURCEEND.getCode()`,导致`if`条件不成立,所以`dlLineageListCopySource`列表没有添加任何数据。
你可以通过在进入`if`条件之前,打印或调试`dlLineage.getSourceLayer()`的值,来确认该属性的实际值。如果为null,可以使用`System.out.println(dlLineage.getSourceLayer())`来进行打印。
另外,你也可以使用断点调试的方式来检查代码的执行流程,以确定是否满足进入`if`条件的条件,并进一步找出问题所在。
相关问题
/*根据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()` 方法,以避免重复获取元素。
/*根据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()))出现问题并给出解决办法
产生`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` 异常的发生。