Monday, 25 April 2016

Iterator vs. ListIterator


Iterator
ListIterator
Can traverses List and Set both
Can traverse List only
Can traverse in only forward direction

Can traverse in both (forward / Backward) directions

Cannot obtain indexes while traversing
Can obtain indexes at any point of time using nextIndex()previousIndex()
Cannot add element to collection while traversing &
Throws ConcurrentModificationException
Can add element at any point of time
Cannot replace the existing element value while traversing
Can replace the value by using ListIterator.set(E e)

Can replace elements returned by next() or previous() methods


Methods of Iterator

  • To Check next element, Go to next , Remove element

hasNext() , next() , remove()

Additional methods of ListIterator
  • To Check previous element, Go to previous, Get next / previous index, Add element, Set value to element
hasPrevious() , previous()
nextIndex() , previousIndex()
add(E e) , set(E e)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.