In previous article, we have discussed various ways to iterate through List i.e.;
Various ways to iterate through List:
- regular for-loop
- Enhanced for-loop introduced in Java 1.5 version
- Iterating using Iterator of Collection interface
- Iterating using ListIterator of List interface
- Read different ways to iterate List
1. Iterating List using enhanced for-loop introduced in Java 1.5 version
- With enhanced for-loop we can iterate through List as demonstrated below,
IteratingListUsingEnhancedForLoop.java
package in.bench.resources.java.collection.list;
import java.util.ArrayList;
import java.util.List;
public class IteratingListUsingEnhancedForLoop {
public static void main(String[] args) {
// creating ArrayList object of type String
List<String> founders = new ArrayList<String>();
// adding elements to ArrayList object
founders.add("Sundar Pichai");
founders.add("Satya Nadella");
founders.add("Shiv Nadar");
founders.add("Shantanu Narayen");
founders.add("Sundar Pichai"); // duplicate object
founders.add("Francisco D’Souza");
founders.add(null); // null is allowed
// Iterating using enhanced for-loop
System.out.println("Iterating using enhanced for-loop\n");
for(String founder : founders) {
System.out.println(founder);
}
}
}
Output:
Iterating using enhanced for-loop
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null
2. Iterating ArrayList using enhanced for-each loop introduced in Java 1.8 version
- In Java 8, we can iterate List using Iterable.forEach() loop
IteratingListUsingForEachLoopInJava8.java
package in.bench.resources.java.collection.list;
import java.util.ArrayList;
import java.util.List;
public class IteratingListUsingForEachLoopInJava8 {
public static void main(String[] args) {
// creating ArrayList object of type String
List<String> founders = new ArrayList<String>();
// adding elements to ArrayList object
founders.add("Sundar Pichai");
founders.add("Satya Nadella");
founders.add("Shiv Nadar");
founders.add("Shantanu Narayen");
founders.add("Sundar Pichai"); // duplicate object
founders.add("Francisco D’Souza");
founders.add(null); // null is allowed
System.out.println("Iterating using forEach() in Java 8\n");
// Iterating using forEach() in Java 8
founders.forEach(founder -> System.out.println(founder));
}
}
Output:
Iterating using forEach() in Java 8
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null
Notice, line no. 25 for forEach statement –> which is doing magic here i.e.; simplifying earlier for-loop
From above example, List
- allows duplicate elements
- null object is allowed
- while iterating insertion-order is maintained
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/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#forEach-java.util.function.Consumer-
- https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
Happy Coding !!
Happy Learning !!