In this article, we will discuss how to iterate/print List (ArrayList/LinkedList) from Java 1.0 to Java 1.8 version.
In one of the previous article, we already discussed about various ways to iterate through ArrayList – 5 ways but here we are going to categorize different ways to iterarte through List based on Java versions.
Different ways to iterate through List :
- for-loop from JDK 1.0 version
- while-loop from JDK 1.0 version
- Iterator interface from Java 1.2 version
- ListIterator interface from Java 1.2 version
- Enhanced for-loop from Java 1.5 version
- List and Collection‘s forEach loop from Java 1.8 version
- Stream‘s forEach loop from Java 1.8 version
1. JDK 1.0 version
- traditional/regular/standard for-loop
- while-loop
1.1 for-loop
- It is the oldest-style of iterating & printing List items
- Steps required are
- initializing an index
- setting up upper limit for index
- incrementing the index-value for every iteration
- List items can be accessed using index-position
IteratingListUsingForLoop.java
package net.bench.resources.iterating.list;
import java.util.ArrayList;
import java.util.List;
public class IteratingListUsingForLoop {
public static void main(String[] args) {
// creating ArrayList object of type String
List<String> names = new ArrayList<String>();
// adding elements to ArrayList object
names.add("Ashneer Grover");
names.add("Aman Gupta");
names.add("Anupam Mittal");
names.add("Ghazal Alagh");
names.add("Namita Thapar");
names.add("Peyush Bansal");
names.add("Vineeta Singh");
// since JDK 1.0 - Iterating using for-loop
System.out.println("Since JDK 1.0 - Iterating using regular for-loop : \n");
for(int index = 0; index < names.size(); index++) {
System.out.println(names.get(index));
}
}
}
Output:
Since JDK 1.0 - Iterating using regular for-loop :
Ashneer Grover
Aman Gupta
Anupam Mittal
Ghazal Alagh
Namita Thapar
Peyush Bansal
Vineeta Singh
1.2 while-loop
- It is also oldest-style of iterating & printing List items
- Steps required are
- initializing an index
- setting up upper limit for index
- incrementing the index-value after/before every iteration (based on requirement)
- List items can be accessed using index-position
IteratingListUsingWhileLoop.java
package net.bench.resources.iterating.list;
import java.util.ArrayList;
import java.util.List;
public class IteratingListUsingWhileLoop {
public static void main(String[] args) {
// creating ArrayList object of type String
List<String> names = new ArrayList<String>();
// adding elements to ArrayList object
names.add("Ashneer Grover");
names.add("Aman Gupta");
names.add("Anupam Mittal");
names.add("Ghazal Alagh");
names.add("Namita Thapar");
names.add("Peyush Bansal");
names.add("Vineeta Singh");
// since JDK 1.0 - Iterating using while-loop
System.out.println("Since JDK 1.0 - Iterating using while-loop : \n");
// initializing counter
int index = 0;
// iterating using while-loop
while(index < names.size()){
// print to console
System.out.println(names.get(index));
index++;
}
}
}
Output:
Since JDK 1.0 - Iterating using while-loop :
Ashneer Grover
Aman Gupta
Anupam Mittal
Ghazal Alagh
Namita Thapar
Peyush Bansal
Vineeta Singh
2. Java 1.2 version
- Iterator interface
- ListIterator interface
2.1 Iterator interface
- This is part of Collection framework introduced in Java 1.2 version which has few useful methods to iterate and print Collection items viz.;
- iterator() – to get the iterator
- hasNext() – to check whether there are any items still left in the Collection
- next() – to get the next item after checking whether there are any items present in Collection using hasNext() method
- Unlike for-loop and while-loop, Iterator interface doesn’t allow to access items based on index-position
- Limitation: iterator allows to move in Forward direction only
IteratingListUsingIterator.java
package net.bench.resources.iterating.list;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratingListUsingIterator {
public static void main(String[] args) {
// creating ArrayList object of type String
List<String> names = new ArrayList<String>();
// adding elements to ArrayList object
names.add("Ashneer Grover");
names.add("Aman Gupta");
names.add("Anupam Mittal");
names.add("Ghazal Alagh");
names.add("Namita Thapar");
names.add("Peyush Bansal");
names.add("Vineeta Singh");
// since Java 1.2 - Iterating using Iterator
System.out.println("Since Java 1.2 - Iterating using Iterator : \n");
// Iterator
Iterator<String> iterator = names.iterator();
// Iterate using Iterator and regular while-loop
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Output:
Since Java 1.2 - Iterating using Iterator :
Ashneer Grover
Aman Gupta
Anupam Mittal
Ghazal Alagh
Namita Thapar
Peyush Bansal
Vineeta Singh
2.2 ListIterator interface
- This is also part of Collection framework introduced in Java 1.2 version specific to List interface which has useful methods to iterate/print List items in both directions viz.; forward and backward
- Forward direction :
- listIterator() – to get list iterator from List
- hasNext() – to check whether there are any items still left in the List in forward direction
- next() – to get the next item after checking whether there are any items present in List using hasNext() method
- Backward direction :
- listIterator() – to get list iterator from List
- hasPrevious() – to check whether there are any items still left in the List in backward direction
- previous() – to get the previous item after checking whether there are any items present in List using hasPrevious() method
- Like Iterator interface, ListIterator also doesn’t allow to access items based on index-position
IteratingListUsingListIterator.java
package net.bench.resources.iterating.list;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class IteratingListUsingListIterator {
public static void main(String[] args) {
// creating ArrayList object of type String
List<String> names = new ArrayList<String>();
// adding elements to ArrayList object
names.add("Ashneer Grover");
names.add("Aman Gupta");
names.add("Anupam Mittal");
names.add("Ghazal Alagh");
names.add("Namita Thapar");
names.add("Peyush Bansal");
names.add("Vineeta Singh");
// ListIterator
ListIterator<String> listIterator = names.listIterator();
// since Java 1.2 - Iterating using ListIterator
System.out.println("1. Since Java 1.2 - Iterating using ListIterator "
+ "in FORWARD direction : \n");
// Forward iterating
while(listIterator.hasNext()){
System.out.println(listIterator.next());
}
// since Java 1.2 - Iterating using ListIterator
System.out.println("\n\n2. Since Java 1.2 - Iterating using ListIterator "
+ "in BACKWARD direction : \n");
// Backward iterating
while(listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
}
}
Output:
1. Since Java 1.2 - Iterating using ListIterator in FORWARD direction :
Ashneer Grover
Aman Gupta
Anupam Mittal
Ghazal Alagh
Namita Thapar
Peyush Bansal
Vineeta Singh
2. Since Java 1.2 - Iterating using ListIterator in BACKWARD direction :
Vineeta Singh
Peyush Bansal
Namita Thapar
Ghazal Alagh
Anupam Mittal
Aman Gupta
Ashneer Grover
3. Java 1.5 version
- enhanced for-loop
3.1 Enhanced for-loop
- This is the new style of writing for-loop where it isn’t required to initialize/increment the starting value
- Moreover, there is no feasibility to access items based on index-postion
- Just access the List items as per the insertion-order while iterating enhanced for-loop
IteratingListUsingEnhancedLoop.java
package net.bench.resources.iterating.list;
import java.util.ArrayList;
import java.util.List;
public class IteratingListUsingEnhancedLoop {
public static void main(String[] args) {
// creating ArrayList object of type String
List<String> names = new ArrayList<String>();
// adding elements to ArrayList object
names.add("Ashneer Grover");
names.add("Aman Gupta");
names.add("Anupam Mittal");
names.add("Ghazal Alagh");
names.add("Namita Thapar");
names.add("Peyush Bansal");
names.add("Vineeta Singh");
// since Java 1.5 - Iterating using for-loop
System.out.println("Since Java 1.5 - Iterating using enhanced for-loop : \n");
// iterating enhanced for each loop
for(String name : names) {
System.out.println(name);
}
}
}
Output:
Since Java 1.5 - Iterating using enhanced for-loop :
Ashneer Grover
Aman Gupta
Anupam Mittal
Ghazal Alagh
Namita Thapar
Peyush Bansal
Vineeta Singh
4. Java 1.8 version
- Collection and List’s forEach loop
- Stream’s forEach loop
4.1 Collection’s forEach loop
- This is the newest style of writing for-loop which takes 1 input parameter called Functional Interface Consumer<E>
- Functional Interface Consumer<E> can be passed in the form of Lambda Expression or Method Reference
IteratingListUsingForEach.java
package net.bench.resources.iterating.list;
import java.util.ArrayList;
import java.util.List;
public class IteratingListUsingForEach {
public static void main(String[] args) {
// creating ArrayList object of type String
List<String> names = new ArrayList<String>();
// adding elements to ArrayList object
names.add("Ashneer Grover");
names.add("Aman Gupta");
names.add("Anupam Mittal");
names.add("Ghazal Alagh");
names.add("Namita Thapar");
names.add("Peyush Bansal");
names.add("Vineeta Singh");
// since Java 1.8 - Iterating using for-loop
System.out.println("Since Java 1.8 - Iterating using forEach : \n\n");
// 1. lambda expression
System.out.println("1. using Lambda expression : \n");
names.forEach(name -> System.out.println(name));
// 2. Method Reference
System.out.println("\n2. using Method Reference : \n");
names.forEach(System.out::println);
}
}
Output:
Since Java 1.8 - Iterating using forEach :
1. using Lambda expression :
Ashneer Grover
Aman Gupta
Anupam Mittal
Ghazal Alagh
Namita Thapar
Peyush Bansal
Vineeta Singh
2. using Method Reference :
Ashneer Grover
Aman Gupta
Anupam Mittal
Ghazal Alagh
Namita Thapar
Peyush Bansal
Vineeta Singh
4.2 Stream.forEach loop
- The newly introduced Stream concept in Java 1.8 version can be used to iterate and print List items
- Unlike Collection‘s forEach loop, this has more features than to just simply iterating and printing Collection/List items i.e.; processing Collection/List items
- Stream.forEach() method accepts 1 input parameter called Functional Interface Consumer<E>
- Functional Interface Consumer<E> can be passed in the form of Lambda Expression or Method Reference
IteratingListUsingStreamForEach.java
package net.bench.resources.iterating.list;
import java.util.ArrayList;
import java.util.List;
public class IteratingListUsingStreamForEach {
public static void main(String[] args) {
// creating ArrayList object of type String
List<String> names = new ArrayList<String>();
// adding elements to ArrayList object
names.add("Ashneer Grover");
names.add("Aman Gupta");
names.add("Anupam Mittal");
names.add("Ghazal Alagh");
names.add("Namita Thapar");
names.add("Peyush Bansal");
names.add("Vineeta Singh");
// since Java 1.8 - Iterating using for-loop
System.out.println("Since Java 1.8 - Iterating using Stream.forEach : \n\n");
// 1. lambda expression
System.out.println("1. using Lambda expression : \n");
names.stream().forEach(name -> System.out.println(name));
// 2. Method Reference
System.out.println("\n2. using Method Reference : \n");
names.stream().forEach(System.out::println);
}
}
Output:
Since Java 1.8 - Iterating using Stream.forEach :
1. using Lambda expression :
Ashneer Grover
Aman Gupta
Anupam Mittal
Ghazal Alagh
Namita Thapar
Peyush Bansal
Vineeta Singh
2. using Method Reference :
Ashneer Grover
Aman Gupta
Anupam Mittal
Ghazal Alagh
Namita Thapar
Peyush Bansal
Vineeta Singh
Related Articles:
- Java 8 – Different ways to iterate through List – 7 ways
- Java 8 – Various ways to iterate through ArrayList – 5 ways
- Java 8 – Iterating List using forEach()
- Java 8 – Iterating Set using forEach()
- Java 8 – Iterating Map using forEach()
- Java 8 – Iterating HashMap in 8 ways
- Java 8 – Various ways to iterate through Hashtable – 6 ways
- Java 8 – Various ways to iterate over HashMap of ArrayList
- Java 8 – Iterate Arrays in 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 !!