Different ways to iterate over an Array List in Java

In this example, we are going to create a ArrayList in Java. Then we will show you on how to fetch each element from ArrayList using different iteration method.

Create a Array List in Java

Output:

Mobile Accessories List : [Headset, Battery, Charger, Cables]

Simple for loop to iterate ArrayList

Enhanced For loop to iterate ArrayList

While loop to iterate ArrayList

While loop using Iterator

While loop using List Iterator

Iterate ArrayList using Lamda expression

Iterate ArrayList using Method expression

For each using stream in Java

Stream forEachOrdered method using lamda

Stream forEachOrdered method using method reference

Output

—– 1) Simple for loop ——-
Headset
Battery
Charger
Cables
—– 2)Enhanced for loop ——-
Headset
Battery
Charger
Cables
—– 3)While loop ——-
Headset
Battery
Charger
Cables
—– 4)While loop using Iterator ——-
Headset
Battery
Charger
Cables
—– 5)While loop using List Iterator ——-
Headset
Battery
Charger
Cables
—– 6)Iterating using lamda expression ——-
Headset
Battery
Charger
Cables
—– 7)Iterating using method expression ——-
Headset
Battery
Charger
Cables
—– 8)Looping using stream ——-
Headset
Battery
Charger
Cables
—– 9)Stream forEachOrdered method by passing lamda expression——-
Headset
Battery
Charger
Cables
—– 10)Stream forEachOrdered method by passing method reference——-
Headset
Battery
Charger
Cables