I currently have the following code
while (!visibleTiles.isEmpty())) {
tile = visibleTiles.keySet().iterator().next();
if (tile != null){
bitmap = visibleTiles.remove(tile);
if(bitmap != null && !containsKey(tile)){ //safe to recycle if tile cache is not actively holding it
bitmap.recycle();
}
}
}
However, I get a NoSuchElementException crash on the line
tile = visibleTiles.keySet().iterator().next();
Is there a big difference in using the isEmpty() method and calling a hasNext() call? I know that hashmaps do not have a hasNext() call so I did the following:
while (visibleTiles.keySet().iterator().hasNext()) {
tile = visibleTiles.keySet().iterator().next();
if (tile != null){
bitmap = visibleTiles.remove(tile);
if(bitmap != null && !containsKey(tile)){ //safe to recycle if tile cache is not actively holding it
bitmap.recycle();
}
}
}
Obviously, I know that I should just run the app and see if it crashes, but the issue is that it’s difficult to reproduce the problem.
Thanks!
visibleTiles.isEmpty()
just checks whether map is empty (or) it has any elements.
retrieves next element from iterator.
You need to do
hasNext()check before doingnext()on iterator.hasNext()javadoc saysso, if no element is available and call
next()on iterator will returnNoSuchElementException.On top of this, I think you really would like to do NOT empty check