How to Iterate List in Java?
List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are ArrayList, LinkedList, Stack, and Vector. In Java, the ArrayList and LinkedList are frequently utilised. We will discover how to iterate a List in Java in this part. This section will use ArrayList only.
Java for Loop
- Basic for Loop and Enhanced for Loop
Java Iterators
- Iterator and ListIterator
Java for Loop
Basic for Loop
The most popular flow control loop for iteration is the Java for loop. A variable that functions as an index number is present in the for loop. It runs till the entire List has finished iterating.
Syntax:
for (initialization ; condition ; increment or decrement)
{
// code to be iterated
}
Iterate1.java
import java.util.*;
// importing all the required packages
public class Iterate1
{
public static void main (String args[])
{
// initializing a list name
List<String> name = Arrays.asList ("kmm", "hyd", "delhi", "nsp", "mdk", "nij");
// iterating over the list name using the for loop
for (int x = 0; x < name.size(); x++)
{
// Displaying the list elements using the get method of list
System.out.println( name.get(i) );
}
}
}
Output:
kmm
hyd
delhi
nsp
mdk
nij
Improved for Loop
It resembles the fundamental for loop. It is readable, simple, and small. It is frequently used to traverse the List. In contrast to a simple for loop, it is simple.
Syntax:
for ( data_type obj : array or collection)
{
// code to be iterated
}
Iterate2.java
// importing all the required packages
import java.util.*;
public class Iterate2
{
public static void main (String args[])
{
// initializing a list name
List<String> name = Arrays.asList("kmm", "hyd", "delhi", "nsp", "mdk", "nij");
// iterating over the list name using the enhanced for loop
for (String obj : name)
{
// Displaying the list elements using the obj
System.out.println( obj );
}
}
}
Output :
kmm
hyd
delhi
nsp
mdk
nij
An iterator in Java
Java offers an interface Iterator for iterating through Collections, including List, Map, and other types. We may iterate through the List by using its two essential methods next() and hasNaxt().
next(): The iteration is carried out in forward order using this method. It gives back the following element in the list. If the iteration doesn't include the List's subsequent element, a NoSuchElementException is thrown. This function can be used in conjunction with previous(): to travel back and forth or repeatedly to iterate through the list.
hasNext(): The hasNext() method enables us to locate the List's final entry. It determines if the next element is present in the List or not. The hasNext() method returns true if it encounters the element while traversing in the forward direction; otherwise, it returns false and ends the execution.
Iterate3.java
// importing all the required packages
import java.util.*;
public class Iterate3
{
public static void main (String args[])
{
// initializing a list name
List<String> name = Arrays.asList("kmm", "hyd", "delhi", "nsp", "mdk", "nij");
// creating an object for iterator class to iterate over list.
Iterator<String> itr = name.iterator();
// iterating over the object till the last using while loop
While (itr.hasNext())
{
// Displaying the list elements using the obj
System.out.println( itr.next() );
}
}
}
Output :
kmm
hyd
delhi
nsp
mdk
nij
ListIterator
Another interface in the java.util package is the ListIterator. The IteratorE> interface is extended. We can go over the List iteratively in either forward or backward order. The identical mechanism that the Iterator uses is provided by the forward iteration over the List. To iterate through the List, we use the next() and hasNext() methods of the Iterator interface.
Iterate4.java
// importing all the required packages
import java.util.*;
public class Iterate4
{
public static void main (String args[])
{
// initializing a list name
List<String> name = Arrays.asList("kmm", "hyd", "delhi", "nsp", "mdk", "nij");
// creating an object for ListIterator class to iterate over list.
ListIterator<String> obj = name.listIterator();
// iterating over the object till the last using while loop
while ( obj.hasNext() )
{
// Displaying the list elements using the obj
System.out.println( obj.next() );
}
}
}
Output :
kmm
hyd
delhi
nsp
mdk
nij