In this post, we will how to iterate list in java. There are some ways in like Java ArrayList iterator() method, splititerator() method , java arraylist foreach() method. Lets see different ways in java iterate ArrayList.
Here is the table content of the article will we will cover this topic.
1. Java ArrayList iterator() method
2. spliterator() method
3. listiterator() method
4. java arraylist foreach() method
Java ArrayList iterator() method
The iterator() method in the ArrayList class returns an object of the iterator that contains all elements of ArrayList. The iterator has elements in random order from what present in the ArrayList. The iterator can iterate the elements of Arraylist in sequential order only.
public Iterator<E> iterator()
This method is declared in Iterable interface but it is overridden in the ArrayList class.
Let’s see in java list iterator example and see how to iterate list in java.
import java.util.ArrayList; import java.util.Iterator; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> listOfNumbers = new ArrayList<Integer>(); listOfNumbers.add(1); listOfNumbers.add(2); listOfNumbers.add(3); listOfNumbers.add(4); listOfNumbers.add(5); listOfNumbers.add(6); listOfNumbers.add(7); listOfNumbers.add(8); Iterator iterator = listOfNumbers.iterator(); System.out.println("Element traversed by iterator: "); while(iterator.hasNext()) System.out.println(iterator.next()); } }
Output: Element traversed by iterator:
1
2
3
4
5
6
7
8
spliterator() method
The spliterator() method returns the object of Spliterator. It creates a late-binding and fail-fast Spliterator. It returns a Spliterator of the same elements as the ArrayList. This method is defined in List interface but it is overridden in ArrayList class. The spliterator object contains all the elements those present in ArrayList.
import java.util.ArrayList; import java.util.Spliterator; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> listOfNumbers = new ArrayList<Integer>(); listOfNumbers.add(1); listOfNumbers.add(2); listOfNumbers.add(3); listOfNumbers.add(4); listOfNumbers.add(5); listOfNumbers.add(6); listOfNumbers.add(7); listOfNumbers.add(8); Spliterator<Integer> iterator = listOfNumbers.spliterator(); System.out.println("Element traversed by iterator: "); iterator.forEachRemaining(ele -> System.out.println(ele)); } }
Output: Element traversed by iterator:
1
2
3
4
5
6
7
8
listiterator() method
The listIterator() method returns an object of listIterator that contains all elements of ArrayList. It returns an iterator of the same elements as the ArrayList. The listIterator() method iterate the elements sequentially. A listIterator can iterate the element in both directions, it means we can move forward or backward. The listIterator() method is overloaded in ArrayList:
public ListIterator<E> listIterator()
Return, this method returns a list iterator over the elements in the ArrayList (in proper sequence).
public ListIterator listIterator(int index)
It is used to return a list iterator over the elements in ArrayList starting at the specified position in the list.
Let’s see how does java listiterator example work.
import java.util.ArrayList; import java.util.ListIterator; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> listOfNumbers = new ArrayList<Integer>(); listOfNumbers.add(1); listOfNumbers.add(2); listOfNumbers.add(3); listOfNumbers.add(4); listOfNumbers.add(5); listOfNumbers.add(6); listOfNumbers.add(7); listOfNumbers.add(8); ListIterator<Integer> iterator1 = listOfNumbers.listIterator(); System.out.println("ListIterator in forward direction: "); while(iterator1.hasNext()) System.out.println(iterator1.next()); System.out.println("ListIterator in backward direction: "); while(iterator1.hasPrevious()) System.out.println(iterator1.previous()); ListIterator<Integer> iterator2 = listOfNumbers.listIterator(1); System.out.println("ListIterator in forward direction: "); while(iterator2.hasNext()) System.out.println(iterator2.next()); System.out.println("ListIterator in backward direction: "); while(iterator2.hasPrevious()) System.out.println(iterator2.previous()); } }
Output: ListIterator in forward direction:
1
2
3
4
5
6
7
8
ListIterator in backward direction:
8
7
6
5
4
3
2
1
ListIterator in forward direction:
2
3
4
5
6
7
8
ListIterator in backward direction:
8
7
6
5
4
3
2
1
java arraylist foreach() method
The forEach(Consumer action) method is used to iterate the ArrayList but it doesn’t return anything because it’s return type is void. It takes one argument of Consumer type. You must read the Consumer interface in Java after that you can understand it easily.
public void forEach(Consumer<? super E> action)
import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> listOfNumbers = new ArrayList<Integer>(); listOfNumbers.add(1); listOfNumbers.add(2); listOfNumbers.add(3); listOfNumbers.add(4); listOfNumbers.add(5); listOfNumbers.add(6); listOfNumbers.add(7); listOfNumbers.add(8); System.out.println("Number traversed from ArrayListed"); listOfNumbers.forEach(number -> System.out.println(number)); } }
Output: Number traversed from ArrayListed
1
2
3
4
5
6
7
8