// Java Program Demonstrate forEach()
// method of ArrayList
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create an ArrayList which going to
// contains a list of Numbers
ArrayList<Integer> Numbers = new ArrayList<Integer>();
// Add Number to list
Numbers.add(23);
Numbers.add(32);
Numbers.add(45);
Numbers.add(63);
// forEach method of ArrayList and
// print numbers
Numbers.forEach((n) -> System.out.println(n));
}
}
forEach() method
@Override
public void forEach(Consumer<? super E> action)
{
Objects.requireNonNull(action);
final int expectedModCount = modCount;
@SuppressWarnings("unchecked")
final E[] elementData = (E[]) this.elementData;
final int size = this.size;
for (int i=0; modCount == expectedModCount && i < size; i++) {
action.accept(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}