The Iterator helps you to move through all the elements in a collection.
The methods used by Iterator are:
| Method |
Description |
boolean hasNext() |
Returns true if the collection has next element, else it returns false. |
E Next() |
Returns the next element. If there is no next element, it throws anexception. |
void remove() |
Removes the current element to which the iterator is pointing. |
- We obtain the
iterator to the start of theCollection by calling iterator()method.
For example, Iterator itr = num.iterator();
The following example illustrates all the methods of Iterator:
import java.util.*;
class IteratorDemo
{
public static void main(String arg[])
{
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
Iterator<Integer> itr = numbers.iterator();
while (itr.hasNext())
{
int number = itr.next();
System.out.print(number + " ");
if (number == 30)
itr.remove();
}
System.out.println("\n..................");
itr = numbers.iterator();//LINE A
while (itr.hasNext())
{
int number = itr.next();
System.out.print(number + " ");
}
}
}
OUTPUT
10 20 30 40 50
..................
10 20 40 50
ListIterator is used to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list.
ListIterator is available to only those collections that implement the Java List Interface . A listIterator has no current element, it's cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().
Advantages of using listIterator over iterator:
- With
iterator you can move only forward, but with listIterator you can also move reverse while reading the elements.
- With
listIterator you can obtain the index at any point while traversing, which is not possible with iterator.
- With
iterator you can only check whether the next element is available or not, but with listIterator you can check the previous and next elements.
- With
listIterator you can add or modify an element at any point while traversing, which is not possible with iterator.
ListIterator Methods:
| Method |
Description |
void add(Object obj) |
Inserts the specified element into the list. |
boolean hasNext( ) |
Returns true if this list iterator has more elements when traversing the list in the forward direction. |
boolean hasPrevious( ) |
Returns true if this list iterator has more elements when traversing the list in the reverse direction. |
Object next( ) |
Returns the next element in the list. |
int nextIndex( ) |
Returns the index of the element that would be returned by a subsequent call to next. |
Object previous( ) |
Returns the previous element in the list. |
int previousIndex( ) |
Returns the index of the element that would be returned by a subsequent call to previous. |
void remove( ) |
Removes from the list the last element that was returned by next or previous. |
void set(Object obj) |
Replaces the last element returned by next or previous with the specified element. |
import java.util.*;
class ListIteratorDemo
{
public static void main(String arg[])
{
ArrayList ar = new ArrayList();
ar.add("Black");
ar.add("Red");
ar.add("Blue");
ListIterator litr = ar.listIterator();
while (litr.hasNext()) // In forward direction
{
System.out.print(litr.next() + " ");
}
System.out.println();
while (litr.hasPrevious()) // In reverse direction
{
System.out.print(litr.previous() + " ");
}
System.out.println();
litr = ar.listIterator(2); // LINE A - Set iterator at specified index
System.out.println(litr.previousIndex() + " " + litr.nextIndex()); // Indices
litr.add("Orange"); // LINE B
System.out.println("After adding Orange : " + litr.previous());
litr.remove(); // LINE C
System.out.println("After removing : " + litr.previous());
litr.set("Yellow"); // LINE D
System.out.println("After setting : " + litr.next());
}
}
OUTPUT
Black Red Blue
Blue Red Black
1 2
After adding Orange : Orange
After removing : Red
After setting : Yellow