In this post, we will see the difference between Iterator and ListIterator in Java. First, we will see Iterator vs ListIterator in brief later we will see each point in details.
Iterator | ListIterator |
---|---|
Iterator is used to traverse List as well Set. | ListIterator is used to traverse List only, we cannot traverse Set using ListIterator. |
In case of Iterator we can traverse only forward direction. | In case of ListIterator, we can traverse in both the directions(forward as well as backward ). |
Iterator has three method. hasNext() next() remove() | ListIterator has following method. add(E e) hasNext() hasPrevious() next() nextIndex() previous() previousIndex() remove() set(E e) |
We cannot modify collection while traversing , it will throws ConcurrentModificationException. | We can modify collection while traversing. |
We don't have any methods in Iterator to get index. | We have nextIndex() and previousIndex() in ListIterator. |
Example of Iterator –
package iteratorandlistitearator; import java.util.*; public class Example { public static void main(String[] args) { List<String> listObject = new ArrayList<>(); listObject.add("ram"); listObject.add("mohan"); listObject.add("shyam"); listObject.add("mohan"); listObject.add("ram"); // iterator() is a method defined in List interface // Iterator is a interface. // it is reference of Iterator interface Iterator it = listObject.iterator(); // it.hasNext() will check, next element is there or not if yes while // loop will execute. while (it.hasNext()) { // it.next() will print all element one by one System.out.println(it.next()); } } }
Output is –
ram
mohan
shyam
mohan
ram
Example of ListIterator –
package iteratorandlistitearator; import java.util.*; public class Example { public static void main(String[] args) { List<String> listObject = new ArrayList<>(); listObject.add("suresh"); listObject.add("mohan"); listObject.add("shyam"); listObject.add("mohan"); listObject.add("ram"); // listIterator() is a method defined in List interface // ListIterator is a interface. // it is reference of ListIterator interface ListIterator it = listObject.listIterator(); // it.hasNext() will check, next element is there or not if yes while // loop will execute. System.out.println("iterating using listiterator forword direction "); while (it.hasNext()) { // it.next() will print all element one by one System.out.println(it.next()); } System.out.println("iterating using listiterator backword direction "); while (it.hasPrevious()) { // it.next() will print all element one by one System.out.println(it.previous()); } } }
Output is –
iterating using listiterator forword direction
suresh
mohan
shyam
mohan
ram
iterating using listiterator backword direction
ram
mohan
shyam
mohan
suresh
We cannot modify a collection while traversing, it will throw ConcurrentModificationException, We can modify a collection while traversing using ListIterator.
package iteratorandlistitearator; import java.util.*; public class Example { public static void main(String[] args) { List<String> listObject = new ArrayList<>(); listObject.add("suresh"); listObject.add("mohan"); listObject.add("shyam"); listObject.add("mohan"); listObject.add("ram"); Iterator it = listObject.iterator(); while (it.hasNext()) { listObject.add("rohan"); System.out.println(it.next()); } System.out.println(listObject); } }
Output is –
Exception in thread “main” java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at iteratorandlistitearator.Example.main(Example.java:19)
package iteratorandlistitearator; import java.util.*; public class Example { public static void main(String[] args) { List<String> listObject = new ArrayList<>(); listObject.add("suresh"); listObject.add("mohan"); listObject.add("shyam"); listObject.add("mohan"); listObject.add("ram"); ListIterator it = listObject.listIterator(); while (it.hasNext()) { it.add("dd"); System.out.println(it.next()); } System.out.println("new list "+listObject); } }
Output is –
suresh
mohan
shyam
mohan
ram
new list [dd, suresh, dd, mohan, dd, shyam, dd, mohan, dd, ram]
Getting index using ListIterator-
package iteratorandlistitearator; import java.util.*; public class Example { public static void main(String[] args) { List<String> listObject = new ArrayList<>(); listObject.add("suresh"); listObject.add("mohan"); listObject.add("shyam"); listObject.add("mohan"); listObject.add("ram"); ListIterator it = listObject.listIterator(); while (it.hasNext()) { System.out.println("next index "+it.nextIndex() + " "+"previous index "+it.previousIndex()); System.out.println(it.next()); } } }
Output is –
next index 0 previous index -1
suresh
next index 1 previous index 0
mohan
next index 2 previous index 1
shyam
next index 3 previous index 2
mohan
next index 4 previous index 3
ram
That’s all about Difference between Iterator and ListIterator in Java.
You may like.
- Difference between Iteration and Enumeration in java.
- Difference between HashSet and HashMap in Java.
- Difference between HashSet and TreeSet in java.
- Difference between List and Set in Java.
- Difference between ArrayList and LinkedList in java.
- ArrayList vs Vector in java.
- How to make List, Set and Map Read Only in Java.
- How to avoid duplicate elements in ArrayList.
- Adding Custom type class object in ArrayList.
- How to Synchronize ArrayList in Java.
- Difference between Arrays and Collections.
- Difference between String and StringBuffer in java.
- Sort HashMap by value in java.
- Sort HashMap by key in java.
- How HashMap works internally in Java.
- How HashSet works internally in java.
- How ArrayList works internally in Java.
- String compareTo() method in java.
- String Constant Pool In Java with Example.
- Performance in case of HashMap and Hashtable
- How get method of ArrayList work internally in java.
Iterator docs.
ListIterator docs.