Differences and Similarities between Iterator and ListIterator in java




In this Collection framework tutorial we will learn what are differences and similarities between java.util.Iterator and java.util.ListIterator in java.



Differences between java.util.Iterator and java.util.ListIterator >



java.util.ListIterator
java.util.Iterator
1
hasPrevious()  method returns true if this listIterator has more elements when traversing the list in the reverse direction.
No such method in java.util.Iterator.
2
previous()  returns previous element in iteration (traversing in backward direction).
if the iteration has no previous elements than NoSuchElementException is thrown.
No such method in java.util.Iterator.
3
nextIndex()  method returns the index of the element that would be returned by a subsequent call to next() method. If listIterator is at the end of the list than method returns size of list.
No such method in java.util.Iterator.
4
previousIndex()  method returns the index of the element that would be returned by a subsequent call to previous() method. If listIterator is at the start of the list than method returns -1.
No such method in java.util.Iterator.
5
add(E element)
Method inserts the specified element into the list.
The element is inserted immediately before the element that would be returned by next (So, subsequent call to next would be unaffected), if any, and after the element that would be returned by previous (So,subsequent call to previous would return the new element), if any.
If the list does not contain any element than new element will be the sole element in the list.
No such method in java.util.Iterator.
6
set(E element)
Method replaces the last element returned by next() or previous() method with the specified element. This call can be made only if neither remove nor add have been called after the last call to next or previous.
If call to set() method is followed up by any call made to remove() or add() method after next() or previous() than UnsupportedOperationException is thrown.
No such method in java.util.Iterator.
7
All the implementations of List interface like ArrayList, LinkedList, Vector, CopyOnWriteArrayList classes returns listIterator.
All Implementation classes of Collection interface’s subinterfaces like Set and List return iterator.



So far we have learned what are differences between java.util.Iterator and java.util.ListIterator in java.
Now we will learn similarities between java.util.Iterator and java.util.ListIterator in Collection framework in java.

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */


Similarity between java.util.Iterator and java.util.ListIterator >

Property
java.util.Iterator
java.util.ListIterator
1
Definition
Iterator method returns iterator to iterate over elements in ArrayList.
ListIterator method returns listIterator to iterate over elements in ArrayList.
2
Common important methods
Iterator important methods :
hasNext() method returns true if the iteration has more elements. (Traversing/Iteration is  done in forward direction).
next() method - returns next element in iteration.
if the iteration has no more elements than NoSuchElementException is thrown.
remove() method - removes last element returned by iterator.
Method must always be called after call to next() method else IllegalStateException is thrown.
ListIterator provide all 3 methods.
Apart from hasNext(), next() and remove() methods provided by Iterator, ListIterator additionally provides(We will discuss that in differences).
3
Complexity
Complexity of Iterator is O(n), because iteration/traversal is done over each and every element.
Complexity of ListIterator is also O(n), because iteration/traversal is done over each and every element.


So in this Collection framework tutorial we will learn what are differences and similarities between java.util.Iterator and java.util.ListIterator in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

ArrayList vs LinkedList - Similarity and Differences in java


HashMap and ConcurrentHashMap - Similarity and Differences in java

Collection vs Collections - Differences in java


HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences in java


HashMap vs IdentityHashMap - Similarity and Differences with program in java


eEdit
Must read for you :