In this article, we will discuss various ways to iterate through ArrayList – 5 ways
Various ways to iterate through ArrayList
- regular for-loop
- Enhanced for-loop introduced in Java 1.5 version
- Iterating using Iterator of Collection interface
- Iterating using ListIterator of List interface
DifferentWaysToIterateArrayList.java
package in.bench.resources.java.collections;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class DifferentWaysToIterateArrayList {
public static void main(String[] args) {
// creating ArrayList object of type String
ArrayList<String> al = new ArrayList<String>();
// adding elements to ArrayList object
al.add("Sundar Pichai");
al.add("Satya Nadella");
al.add("Shiv Nadar");
al.add("Shantanu Narayen");
al.add("Sundar Pichai"); // duplicate object
al.add("Francisco D’Souza");
al.add(null); // null is allowed
// Way 1: Iterating using for-loop
System.out.println("Way 1: Iterating using for-loop\n");
for(int index = 0; index < al.size(); index++) {
System.out.println(al.get(index));
}
// Way 2: Iterating using enhanced for-loop
System.out.println("\n\nWay 2: Iterating using "
+ "enhanced for-loop\n");
for(String str : al) {
System.out.println(str);
}
// Way 3: Iterating using Iterator of Collection interface
System.out.println("\n\nWay 3: Iterating using "
+ "Iterator of Collection interface\n");
Iterator<String> itr = al.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
// Way 4: Iterating using ListIterator of List interface
System.out.println("\n\nWay 4.A: Iterating using ListIterator of "
+ "List interface in FORWARD direction\n");
ListIterator<String> lstItr = al.listIterator();
while(lstItr.hasNext()) {
System.out.println(lstItr.next());
}
System.out.println("\n\nWay 4.B: Iterating using ListIterator of "
+ "List interface in BACKWARD direction\n");
while(lstItr.hasPrevious()) {
System.out.println(lstItr.previous());
}
}
}
Output:
Way 1: Iterating using for-loop
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null
Way 2: Iterating using enhanced for-loop
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null
Way 3: Iterating using Iterator of Collection interface
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null
Way 4.A: Iterating using ListIterator of List interface in FORWARD dir
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null
Way 4.B: Iterating using ListIterator of List interface in BACKWARD dir
null
Francisco D’Souza
Sundar Pichai
Shantanu Narayen
Shiv Nadar
Satya Nadella
Sundar Pichai
From above example, ArrayList
- allows duplicate elements
- null object is allowed
- while iterating insertion order is maintained
In the next article, we will see a demo example on how to iterate List using Stream in Java 1.8 i.e.;
Related Articles:
To conclude, now there are 5 ways to iterate List
- for-loop (regular)
- Enhanced for-loop (introduced in Java 1.5 version)
- Iterating using Iterator of Collection interface
- Iterating using ListIterator of List interface
- Iterating List using forEach() in Java 8
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Enumeration.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html
Happy Coding !!
Happy Learning !!