Is it better to use a for-each loop or an Iterator with a collection?

John Zukowski

It depends on what you need to do. The remove() method of an Iterator is the safe way to remove elements from an underlying collection. You can't safely remove elements with a for-each loop. For just "visiting" each element, either way works.

About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.