In this section we will learn what the ArrayList iterator(), and listIterator() methods are and how to use them in Java.
What is Java ArrayList iterator() Method?
The Java ArrayList iterator() method is used to iterate through the elements of a list object.
This method returns an object that implemented the Iterator interface.
This means we have these methods at our disposal after calling the iterator() method:
- forEachRemaining(): this method is like the forEach() method and using it we can create a set of instructions to be executed on every remained elements of a list object.
- hasNext(): when iterating through a list object, we can use this method to see if there’s still element left in the target list object to iterate. Calling this method returns a Boolean value. If the result was true, that means there’s still an element in the target list. Otherwise, if there was no element left, the return value of this method becomes false.
- next(): calling this method will give us the next element in the target list.
- remove(): this method could be used to remove the next element in a list object.
Java ArrayList iterator() Method Syntax:
Iterator<E> iterator()
iterator() Method Parameters:
The method does not take an argument.
iterator() Method Return Value:
The return value of this method is an object that implemented the Iterator interface.
iterator() Method Exceptions:
The method does not throw an exception.
Example: using ArrayList iterator() method
import java.util.List; import java.util.ArrayList; import java.util.Iterator; public class Main { public static void main(String[] args){ List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()){ int i = iterator.next(); System.out.print(i+", "); if (i >5){ iterator.remove(); } } System.out.println("\nHere are the elements of the list after iteration: "); list.forEach(e-> System.out.print(e+", ")); } }
Output:
1, 2, 3, 4, 5, 6, 7, Here are the elements of the list after iteration: 1, 2, 3, 4, 5,
As you can see, using the Iterator object, we can iterate a list and remove its element if needed.
What is Java ArrayList listIterator() Method?
The Java ArrayList listIterator() method is used to iterate the elements of a list object.
The difference between this method and the iterator() method is that the return value from the listIterator() method is an object that allows us to iterate through the elements of a list backward as well.
Also using the ListIterator object (the return object of this method) we can add or replace an element in a list object.
Java ArrayList listIterator() Method Syntax:
ListIterator<E> listIterator()
listIterator() Method Parameters:
The method does not take an argument.
listIterator() Method Return Value:
The return value of this method is a ListIterator object by which we can iterate through the elements of a list backward and forward.
listIterator() Method Exceptions:
The method does not throw an exception.
Example: using ArrayList listIterator() method
import java.util.List; import java.util.ArrayList; import java.util.ListIterator; public class Main { public static void main(String[] args){ List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); ListIterator<Integer> iterator = list.listIterator(); while(iterator.hasNext()){ int i = iterator.next(); System.out.print(i+", "); if (i >5){ iterator.set(i*50); } } System.out.println("\nHere are the elements of the list after iteration: "); list.forEach(e-> System.out.print(e+", ")); } }
Output:
1, 2, 3, 4, 5, 6, 7, Here are the elements of the list after iteration: 1, 2, 3, 4, 5, 300, 350,