Different ways to iterate over an Array List in Java
Contents
- 1 Create a Array List in Java
- 1.1 Simple for loop to iterate ArrayList
- 1.2 Enhanced For loop to iterate ArrayList
- 1.3 While loop to iterate ArrayList
- 1.4 While loop using Iterator
- 1.5 While loop using List Iterator
- 1.6 Iterate ArrayList using Lamda expression
- 1.7 Iterate ArrayList using Method expression
- 1.8 For each using stream in Java
- 1.9 Stream forEachOrdered method using lamda
- 1.10 Stream forEachOrdered method using method reference
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.*; public class MobilePhone { public static void main(String[] args) { // Declaring String ArrayList variable ArrayList<String> accessories = new ArrayList<String>(); //Adding elements to Array list accessories.add("Headset"); accessories.add("Battery"); accessories.add("Charger"); accessories.add("Cables"); System.out.println("Mobile Accessories List : " + accessories); } } |
Output:
Mobile Accessories List : [Headset, Battery, Charger, Cables]
Simple for loop to iterate ArrayList
1 2 3 4 |
System.out.println("----- 1) Simple for loop -------"); for(int i=0;i<accessories.size();i++){ System.out.println(accessories.get(i)); } |
Enhanced For loop to iterate ArrayList
1 2 3 4 |
System.out.println("----- 2)Enhanced for loop -------"); for(String eachAccessory : accessories){ System.out.println(eachAccessory); } |
While loop to iterate ArrayList
1 2 3 4 5 6 |
System.out.println("----- 3)While loop -------"); int i=0; while (i < accessories.size() ){ System.out.println(accessories.get(i)); i++; } |
While loop using Iterator
1 2 3 4 5 |
System.out.println("----- 4)While loop using Iterator -------"); Iterator<String> accessoriesIterator = accessories.iterator(); while (accessoriesIterator.hasNext()){ System.out.println(accessoriesIterator.next()); } |
While loop using List Iterator
1 2 3 4 5 |
System.out.println("----- 5)While loop using List Iterator -------"); ListIterator<String> accessoriesListIterator = accessories.listIterator(); while (accessoriesListIterator.hasNext()){ System.out.println(accessoriesListIterator.next()); } |
Iterate ArrayList using Lamda expression
1 2 |
System.out.println("----- 6)Iterating using lamda expression -------"); accessories.forEach(accessory -> System.out.println(accessory)); |
Iterate ArrayList using Method expression
1 2 |
System.out.println("----- 7)Iterating using method expression -------"); accessories.forEach(System.out::println); |
For each using stream in Java
1 2 |
System.out.println("----- 8)Looping using stream -------"); accessories.stream().forEach( accessory -> System.out.println(accessory)); |
Stream forEachOrdered method using lamda
1 2 |
System.out.println("----- 9)Stream forEachOrdered method by passing lamda expression-------"); accessories.stream().forEachOrdered(accessory -> System.out.println(accessory)); |
Stream forEachOrdered method using method reference
1 2 |
System.out.println("----- 10)Stream forEachOrdered method by passing method reference-------"); accessories.stream().forEachOrdered(System.out::println); |
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