How to iterate List in Java
In this topic, we are going to learn to iterate the List in Java. A List is an interface that is available in java.util package. These are the following ways to iterate the List:
• Using For loop
• Using For-each loop
• Using While loop
• Using Iterator
• Using ListIterator
• Using Lambda Expression
• Using stream.forEach()
Using For loop
Example
import java.util.List;
import java.util.ArrayList;
public class Test {
public static void main(String args[]) {
//Creating an ArrayList
List < String > listofP = new ArrayList < String > ();
//Adding elements to the List
listofP.add("Peter");
listofP.add("Robert");
listofP.add("John");
listofP.add("Parker");
//Using for loop to iterate the elements of the List
for (int p = 0; p < listofP.size(); p++) {
System.out.println(listofP.get(p));
}
}
}
Output
Peter
Robert
John
Parker
Using For-each loop
Example
import java.util.List;
import java.util.ArrayList;
public class Test {
public static void main(String args[]){
//Creating an ArrayList
List<String> theList = new ArrayList<String>();
//Adding elements to the List
theList.add("Peter");
theList.add("Robert");
theList.add("John");
theList.add("Parker");
//Using for each loop to iterate the List
for (String name:theList) {
System.out.println(name);
}
}
}
Output
Peter
Robert
John
Parker
Using While loop
Example
import java.util.List;
import java.util.ArrayList;
public class Test {
public static void main(String args[]){
//Creating an ArrayList
List<String> theList = new ArrayList<String>();
//Adding elements to the List
theList.add("Peter");
theList.add("Robert");
theList.add("John");
theList.add("Parker");
int i=0;
//Using while loop to iterate the List
while(i<theList.size()) {
System.out.println(theList.get(i));
i++;
}
}
}
Output
Peter
Robert
John
Parker
Using Iterator
Example
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String args[]){
//Creating an ArrayList
List<String> theList = new ArrayList<String>();
//Adding elements to the List
theList.add("Peter");
theList.add("Robert");
theList.add("John");
theList.add("Parker");
int i=0;
//Using Iterator to iterate the List
Iterator<String> it = theList.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
Output
Peter
Robert
John
Parker
Using ListIterator
Example
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
public class Test {
public static void main(String args[]){
//Creating a ArrayList
List<String> theList = new ArrayList<String>();
//Adding elements to the List
theList.add("Peter");
theList.add("Robert");
theList.add("John");
theList.add("Parker");
//List Iterator
ListIterator<String> it = theList.listIterator();
//Using List Iterator to iterate the List
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
Output
Peter
Robert
John
Parker
Using Lambda expression
Example
import java.util.List;
import java.util.ArrayList;
public class Test {
public static void main(String args[]){
//Creating an ArrayList
List<String> theList = new ArrayList<String>();
//Adding elements to the List
theList.add("Peter");
theList.add("Robert");
theList.add("John");
theList.add("Parker");
//Using Lambda Expression to iterate the List
theList.forEach(
(name) -> { System.out.println(name); });
}
}
Output
Peter
Robert
John
Parker
Using stream.forEach()
Example
import java.util.List;
import java.util.ArrayList;
public class Test {
public static void main(String args[]){
//Creating an ArrayList
List<String> theList = new ArrayList<String>();
//Adding elements to the List
theList.add("Peter");
theList.add("Robert");
theList.add("John");
theList.add("Parker");
//Using stream.forEach() to iterate the List
theList.stream().forEach(
(name) -> System.out.println(name));
}
}
Output
Peter
Robert
John
Parker
Conclusion
In this topic, we learnt how to iterate the List in several ways in Java.
Leave your thought here
Your email address will not be published. Required fields are marked *