Java ArrayList Iterator example shows how to get Iterator over ArrayList elements in Java. The example also shows how to iterate ArrayList using hasNext and next methods of Iterator.
How to get ArrayList Iterator?
The ArrayList class implements Iterable interface hence it provides iterator
method which can be used to get the Iterator object over its elements.
1 |
public Iterator<T> iterator() |
This method returns an Iterator object over ArrayList elements of type T.
How to Iterate ArrayList using Iterator object?
Once we get the Iterator object from the ArrayList, we can use hasNext
and next
methods of Iterator to iterate through the ArrayList.
1 |
boolean hasNext() |
This method returns true if the Iterator has more elements.
1 |
E next() |
This method returns the next element in the iteration.
Note: next()
method may throw NoSuchElementException
if there are no more elements. Always check if there are more elements to iterate using hasNext()
method before making call to the next()
method to avoid this exception.
ArrayList Iterator example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.Iterator; public class ArrayListIteratorExample { public static void main(String[] args) { //Create an ArrayList ArrayList<String> aListColors = new ArrayList<String>(); //Add elements aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); aListColors.add("Yellow"); /* * To get an Iterator, use * public Iterator<E> iterator() * method of ArrayList class. */ Iterator<String> itr = aListColors.iterator(); /* * Use hasNext() method of iterator * to check if there are any more elements * and next() method to get the next element * from Iterator */ while( itr.hasNext() ) System.out.println( itr.next() ); } } |
Output
1 2 3 4 |
Red Green Blue Yellow |
How to remove elements while iterating through the ArrayList?
You can call remove
method of Iterator class to remove elements from the ArrayList.
1 |
void remove() |
This method removes the last element returned by the Iterator from the underlying collection.
Note: This method must be called once per next
method call. It may throw IllegalStateException
if remove
method is already called after the last call to the next
method or next
method is not called at all. It may also throw UnsupportedOperationException
if remove operation is not supported by the underlying collection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Iterator<String> itr = aListColors.iterator(); System.out.println("ArrayList contains: " + aListColors); String strColor; while( itr.hasNext() ){ //get an element from iterator using next method strColor = itr.next(); //remove element from ArrayList using remove method of Iterator if(strColor.equals("Blue")) itr.remove(); } System.out.println("ArrayList contains: " + aListColors); |
Output
1 2 |
ArrayList contains: [Red, Green, Blue, Yellow] ArrayList contains: [Red, Green, Yellow] |
Note: The iterator object returned by the iterator
method of the ArrayList class is fail-fast. It means that if the original ArrayList is structurally modified after getting the Iterator from it in any way except for the Iterator’s own methods, the iterator will throw ConcurrentModificationException
.
Also, see the how to iterate ArrayList using various methods example.
This example is a part of the Java ArrayList tutorial with examples.
Please let me know your views in the comments section below.