In this article, we will discuss various ways to iterate through LinkedList – 5 ways
Various ways to iterate through LinkedList
- Regular for-loop
- Enhanced for-loop introduced in Java 1.5 version
- Iterating using Iterator of Collection interface
- Iterating using ListIterator of List interface
- forEach() loop introduced in Java 1.8 version
DifferentWaysToIterateLinkedList.java
package in.bench.resources.collection;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;
public class DifferentWaysToIterateLinkedList {
public static void main(String[] args) {
// creating LinkedList object of type String
LinkedList<String> ll = new LinkedList<String>();
// adding elements to LinkedList object
ll.add("Samsung");
ll.add("iPhone");
ll.add("OnePlus");
ll.add("Motorola");
ll.add("iPhone"); // duplicate object
ll.add("Micromax");
ll.add(null); // null is allowed
// Way 1: Iterating using regular for-loop
System.out.println("Way 1: Iterating using"
+ " Regular for-loop\n");
for(int index = 0; index < ll.size(); index++) {
System.out.println(ll.get(index));
}
// Way 2: Iterating using enhanced for-each loop
System.out.println("\n\nWay 2: Iterating using"
+ " enhanced for-each loop\n");
for(String str : ll) {
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 = ll.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 = ll.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());
}
// Way 5: Iterating using forEach loop in Java 1.8
System.out.println("\n\nWay 5: Iterating using"
+ " forEach loop in Java 1.8\n");
ll.forEach(fone -> System.out.println(fone));
}
}
Output:
Way 1: Iterating using Regular for-loop
Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null
Way 2: Iterating using enhanced for-each loop
Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null
Way 3: Iterating using Iterator of Collection interface
Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null
Way 4.A: Iterating using ListIterator of List interface in FORWARD direction
Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null
Way 4.B: Iterating using ListIterator of List interface in BACKWARD direction
null
Micromax
iPhone
Motorola
OnePlus
iPhone
Samsung
Way 5: Iterating using forEach loop in Java 1.8
Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null
From above example, LinkedList
- allows duplicate elements
- null object is allowed
- while iterating insertion-order is maintained
Related Articles:
- Java – Various ways to iterate through ArrayList
- Java – Various ways to iterate through Vector – 5 ways
- Java – Various ways to iterate through LinkedList – 5 ways
- Java – Various ways to iterate through HashSet – 3 ways
- Java – How to reverse LinkedHashSet contents ?
- Java – Various ways to iterate through TreeSet – 3 ways
- Java – Various ways to iterate over List of HashMap
- Java – Ways to iterate over HashMap of ArrayList
- Java – Various ways to iterate Arrays – 5 ways
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 !!