Difference between Iterator and ListIterator in java

In this post, we will see difference between Iterator and ListIterator in java. They are both used for traversal but it is good to point out differences between them.

Iterator vs ListIterator:

Parameter
Iterator
ListIterator
Traversal
Iterator can be used to traverse List,set, Queue
ListIterator can be used to traverse only List.
Traversal direction

Iterator can be traversed only in forward direction

ListIterator can be traverse in forward direction as well backward.
Adding elements while traversing
You can not add elements while traversing using Iterator. It will through ConcurrentModificationException
You can add elements while traversing using ListIterator.
Replacing existing elements 
You can not replace exisitng elements while traversing using Iterator.
You can replace exisitng elements while traversing using ListIterator using set(E e).
Accessing Index
You can not access index using Iterator
You can access index using ListIterator via nextIndex() or previousIndex methods

Example:

1. Country.java 

package org.arpit.java2blog;
public class Country {

    String name;
    long population;

    public Country(String name, long population) {
        super();
        this.name = name;
        this.population = population;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public long getPopulation() {
        return population;
    }
    public void setPopulation(long population) {
        this.population = population;
    } 
}

2. IterateListMain.java 

package org.arpit.java2blog;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class IterateListMain {
    /**
     * @author Arpit Mandliya
     */
    public static void main(String[] args) {

        Country india=new Country("India",1000);
        Country japan=new Country("Japan",10000);

        Country france=new Country("France",2000);
        Country russia=new Country("Russia",20000);

        // We are going to iterate on this list and will print 
        //name of the country 
        ArrayList countryLists=new ArrayList();
        countryLists.add(india);
        countryLists.add(japan);
        countryLists.add(france);
        countryLists.add(russia);

        System.out.println("-----------------------------");

        // Iterator
        System.out.println("Iterating using iterator : ");
        Iterator iteratorCountryLists= countryLists.iterator();
        while(iteratorCountryLists.hasNext())
        {
            System.out.println(iteratorCountryLists.next().getName());
        }

        System.out.println("-----------------------------");
        //List iterator

        System.out.println("Iterating using ListIterator in forward direction : ");
        ListIterator listIteratorCountry= countryLists.listIterator();
        while(listIteratorCountry.hasNext())
        {
            System.out.println(listIteratorCountry.next().getName());
        }

        System.out.println("-----------------------------");
        System.out.println("Iterating using ListIterator in backward direction : ");

        while(listIteratorCountry.hasPrevious())
        {
            System.out.println(listIteratorCountry.previous().getName());
        }

    }
}

When you run above program, you will get following output:

-----------------------------
Iterating using iterator : 
India
Japan
France
Russia
-----------------------------
Iterating using ListIterator in forward direction : 
India
Japan
France
Russia
-----------------------------
Iterating using ListIterator in backward direction : 
Russia
France
Japan
India

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.