Table of Contents
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.
|
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Â
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 |
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; } } |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
----------------------------- 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 |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.
how to create multi threading