Easy Tutorial
For Competitive Exams

Java Programming Iterator and ListIterator

What is Iterator?

All of the collection classes provides iterator() method to iterate through the collection.

The iterator() method returns the Iterator object through which you can access the collection elements in an order.

How to iterate through collection objects?

We can iterate through any collection object by using Iterator object.

Two methods are used to iterate.

The hasNext() method returns true if the iteration has more elements.

The next() method returns the next element in the iteration.

Example

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IteratorDemo {
    public static void main(String a[]){
        List myList = new ArrayList();
        myList.add("C");
        myList.add("C++");
        myList.add("Java");
        myList.add("C#");
        myList.add("Android");
        Iterator itr = myList.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());
        }
    }
}
Try it Yourself

How to remove an element from collection using Iterator object?

The remove() method is used to remove an element from collection object using Iterator object.

Example

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RemoveElement {
    public static void main(String a[]){
        String removeElem = "Android";
        List myList = new ArrayList();
        myList.add("C");
        myList.add("C++");
        myList.add("Java");
        myList.add("C#");
        myList.add("Android");
        System.out.println("Before remove:");
        System.out.println(myList);
        Iterator itr = myList.iterator();
        while(itr.hasNext()){
            if(removeElem.equals(itr.next())){
                itr.remove();
            }
        }
        System.out.println("After remove:");
        System.out.println(myList);
    }
}
Try it Yourself

What is ListIterator?

Using ListIterator, we can iterate all elements of a list in either direction.

We can access next element by calling next() method, and also we can access previous element by calling previous() method on the list.

Example

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
 
public class MyListIterator {
    public static void main(String a[]){
        List li = new ArrayList();
        ListIterator litr = null;
        li.add(23);
        li.add(98);
        li.add(29);
        li.add(71);
        li.add(5);
        litr=li.listIterator();
        System.out.println("Elements in forward directiton");
        while(litr.hasNext()){
            System.out.println(litr.next());
        }
        System.out.println("Elements in backward directiton");
        while(litr.hasPrevious()){
            System.out.println(litr.previous());
        }
    }
}
Try it Yourself

Difference Between Iterator and ListIterator

BASIS FOR COMPARISON ITERATOR LISTITERATOR
Basic Iterator can traverse the elements in a collection only in forward direction. ListIterator can traverse the elements in a collection in forward as well as the backwards direction.
Add Iterator is unable to add elements to a collection. ListIteror can add elements to a collection.
Modify Iterator can not modify the elements in a collection. ListIterator can modify the elements in a collection using set().
Traverse Iterator can traverse Map, List and Set. ListIterator can traverse List objects only.
Index Iterator has no method to obtain an index of the element in a collection. Using ListIterator, you can obtain an index of the element in a collection.

Solve this!!

#20979,20980,20981,20982,20983
Share with Friends