Iterate through List in Java
Lists in java allow us to maintain an ordered collection of objects. Duplicate elements as well as null elements can also be stored in a List in Java. The List interface is a part of java.util package and it inherits the Collection interface. It preserves the order of insertion.
There are several ways to iterate over List in Java. They are discussed below:
Methods:
- Using loops (Naive Approach)
- Using Iterator
- Using List iterator
- Using lambda expression
- Using stream.forEach()
Method 1-A: Simple for loop
Each element can be accessed by iteration using a simple for loop. The index can be accessed using the index as a loop variable.
Syntax:
for (i = 0; i < list_name.size(); i++)
{
// code block to be executed
}
Example
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
List<String> myList = new ArrayList<String>();
myList.add( "A" );
myList.add( "B" );
myList.add( "C" );
myList.add( "D" );
for ( int i = 0 ; i < myList.size(); i++) {
System.out.println(myList.get(i));
}
}
}
|
Method 1-B: Enhanced for loop
Each element can be accessed by iteration using an enhanced for loop. This loop was introduced in J2SE 5.0. It is an alternative approach to traverse the for a loop. It makes the code more readable.
Syntax:
for(data_type variable : List_name)
{
// Body of the loop.
// Each element can be accessed using variable.
}
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
List<String> myList = new ArrayList<String>();
myList.add( "A" );
myList.add( "B" );
myList.add( "C" );
myList.add( "D" );
for (String i : myList) {
System.out.println(i);
}
}
}
|
Method 1-C: Using a while loop
Iterating over a list can also be achieved using a while loop. The block of code inside the loop executes until the condition is true. A loop variable can be used as an index to access each element.
Syntax:
while(variable<list_name.size())
{
// Block of code to be executed
}
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
List<String> myList = new ArrayList<String>();
myList.add( "A" );
myList.add( "B" );
myList.add( "C" );
myList.add( "D" );
int i = 0 ;
while (i < myList.size()) {
System.out.println(myList.get(i));
i++;
}
}
}
|
Method 2: Using iterator
An iterator is an object in Java that allows iterating over elements of a collection. Each element in the list can be accessed using iterator with a while loop.
Syntax:
Iterator<data_type> variable = list_name.iterator();
Example
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
List<String> myList = new ArrayList<String>();
myList.add( "A" );
myList.add( "B" );
myList.add( "C" );
myList.add( "D" );
Iterator<String> it = myList.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
|
Method 3: Using List iterator
ListIterator is an iterator is a java which is available since the 1.2 version. It allows us to iterate elements one-by-one from a List implemented object. It is used to iterator over a list using while loop.
Syntax
ListIterator<data_type> variable = list_name.listIterator();
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
List<String> myList = new ArrayList<String>();
myList.add( "A" );
myList.add( "B" );
myList.add( "C" );
myList.add( "D" );
ListIterator<String> it = myList.listIterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
|
Method 4: Using Iterable.forEach()
This feature is available since Java 8. It can also be used to iterate over a List. Iteration can be done using a lambda expression.
Syntax:
list_name.forEach(variable->{//block of code})
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
List<String> myList = new ArrayList<String>();
myList.add( "A" );
myList.add( "B" );
myList.add( "C" );
myList.add( "D" );
myList.forEach(
(temp) -> { System.out.println(temp); });
}
}
|
Method 5: Using Stream.forEach()
The processing order of stream().forEach() is undefined while in case of forEach(), it is defined. Both can be used to iterate over a List.
Syntax:
list_name.stream.forEach(variable->{//block of code})
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
List<String> myList = new ArrayList<String>();
myList.add( "A" );
myList.add( "B" );
myList.add( "C" );
myList.add( "D" );
myList.stream().forEach(
(temp) -> System.out.println(temp));
}
}
|
Last Updated :
19 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...