You can learn more about the Consumer here. You have to pass the Consumer for the parameter. You can use the forEach on the List directly. And it is recommended to use an enhanced for-loop instead of this. This may not be a good idea, since we should focus on immutability to have a maintainable code to reduce errors and debugging time in the future. In both cases, Iterator and ListIterator, you can modify the list along the way. } Code language: PHP ( php ) The drawback of Iterator and ListIterator ListIterator listIterator = list.listIterator()
You can move backward as well and modify the current element as well.
ListIterator is kind of similar to the Iterator but has a lot more functionality. } Code language: PHP ( php ) ListIterator In Java
for (int i = 0 i and you can traverse through it via next(). for-loop in Javaįor Loop, you integrate a list using an index. These are your for-loop, while-loop, and enhanced for-loop. List list = Arrays.asList( 1, 2, 3, 4, 5, 6, 6, 7, 8, 10) Code language: PHP ( php ) Basic Level of Loopsįirst, let’s look at the basic level of looping. I will use the same list throughout all the code samples.