Iterator嵌套循环遍历迭代
private void filterMengGameById(List<Game> games,List<Long> gameIds){
if (CollectionUtils.isEmpty(gameIds) || CollectionUtils.isEmpty(games)){
return;
}
//获取迭代器
Iterator<Game> gameIterator = games.iterator();
while (gameIterator.hasNext()){
Game game = gameIterator.next();
//获取迭代器
Iterator<Long> idIterator = gameIds.iterator();
while (idIterator.hasNext()){
Long id = idIterator.next();
if (game.getGameId().equals(id)){
idIterator.remove();
}
}
}
}
注意:
1、获取迭代器的位置一定要放对地方
2、外循环一次,内循环完所有次数