
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate Over a List in Java
In Java if you try to print a List (or any) object directly using the print statement a hash-code of the object will be printed (default behaviour).
If you want to print each element of your object (such as List, Array, etc.) or perform any operation on them one by one, you need to iterate through it.
Java provides various ways to iterate through objects. Following are different approaches to iterate through a List object:
Iterate over a List using For Loop
A for loop is a control-flow statement that is used to execute a block of code repeatedly till the condition is not becomes false.
To iterate over a List, we will run the loop at an initial value 0, and continue until it reaches the last index of the list. You can use the get(index) method to access the List elements.
Example
In the following example, we iterate over a List using a for-loop and use the get(index) method inside the loop to access each element based on the indexed value passed to it:
import java.util.ArrayList; import java.util.List; public class iterateOverList { public static void main(String[] args) { //instantiating a List using ArrayList class List<Integer> list = new ArrayList<>(); //adding element to it list.add(1); list.add(2); list.add(3); list.add(4); System.out.println("The list elements are: "); //iterate over a list using for-loop for(int i = 0; i<list.size(); i++){ System.out.println(list.get(i)); } } }
The above program produces the following output:
The list elements are: 1 2 3 4
Using while Loop
A while loop is also a control flow statement, which executes a block of code repeatedly until the specified condition is true. Once the condition is false, it will stop executing.
In the while loop, we compare the initial value (i.e., i = 0) with the size() of the list ( i < size) and execute the block of code until the condition does not become false (i.e., i > size).
Example
In the example below, we use the while loop to iterate over a List and use the get(index) method to access each element of the List {"Apple", "Banana", "Orange", "Grapes"}:
import java.util.ArrayList; import java.util.List; public class iterateOverList { public static void main(String[] args) { //instantiating a List using ArrayList class List<String> fruits = new ArrayList<>(); //adding element to it fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); fruits.add("Grapes"); System.out.println("The list elements are: "); //iterate over a fruits using while-loop int i = 0; while(i < fruits.size()){ System.out.println(fruits.get(i)); i++; } } }
Following is the output of the above program:
The list elements are: Apple Banana Orange Grapes
Using an Iterator Object
In Java, an Iterator is an object that is used to loop through collections objects, such as ArrayList, HashSet, etc. It provides a method named iterator(), which retrieves an iterator.
Example
In this example, we use an Iterator object to iterate over a List and use the next() method inside a while loop to print each element of the List one by one:
import java.util.ArrayList; import java.util.Iterator; public class iterateOverList { public static void main(String[] args) { //instantiating a List using ArrayList class ArrayList<String> list = new ArrayList<String>(); //adding element to it list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); System.out.println("The list elements are: "); //Iterator object Iterator iterator = list.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } } }
Below is the output of the above program:
The list elements are: JavaFx Java WebGL OpenCV