[英]Iterator.remove() is not working while removing values from list
I have a list let say car and i have to remove the values from the list based on certain condition so if the value is null then i have to remove the value from the list.我有一个列表,比如说汽车,我必须根据特定条件从列表中删除值,所以如果值为空,那么我必须从列表中删除该值。 My code looks like :
我的代码看起来像:
List<CarDTO> carList //list
Iterator<CarDTO> itr = carList.iterator();
while (itr.hasNext())
{
CarDTO car = itr.next();
String carText = car.getCarText();
carText = replaceCarTextWithCompanyName(carText, readData);
if (CommonUtils.isEmpty(carText))
{
itr.remove(); //this is not working, values are still getting displayed
}
else
{
car.setCarText(carText);
}
}
Here you have an example:这里有一个例子:
List<String> names = new ArrayList<>(Arrays.asList("Bob", "Don", null, "Mark", null, "John"));
List<String> newNames = names.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList())
;
newNames.forEach(System.out::println);
I have a list let say car and i have to remove the values from the list based on certain condition so if the value is null then i have to remove the value from the list.我有一个列表,可以说是car,我必须根据特定条件从列表中删除值,因此,如果该值为null,那么我必须从列表中删除该值。 My code looks like :
我的代码如下所示:
List<CarDTO> carList //list
Iterator<CarDTO> itr = carList.iterator();
while (itr.hasNext())
{
CarDTO car = itr.next();
String carText = car.getCarText();
carText = replaceCarTextWithCompanyName(carText, readData);
if (CommonUtils.isEmpty(carText))
{
itr.remove(); //this is not working, values are still getting displayed
}
else
{
car.setCarText(carText);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.