3 ways to Loop through a List in Java
There are multiple ways to traverse or loop through a List in Java e.g. by using an Iterator , by using an enhanced for loop of Java 5, and not the forEach() method of Java 8. Given a List is an index based collection, if you know the index you can retrieve an object from List and because of this you can also use traditional for loop which keeps count for iterating List. Now the question is whether should you use the Iterator or enhanced for loop, or the forEach() method of Java 8 for looping over List in Java. Well, it depends on what you are doing with the object, if you need to remove some objects from List than iterating using Iterator is best choice to avoid ConcurrentModificationException, but if you are not removing any element and just doing some operation with each element than enhanced for loop is much cleaner ways to do that.