ListIterator-interface-in-collection-framework

Java List Interface

List is a sub interface of Collection interface. A List represents indexed collection of elements which can be referred to an indexed. List allows duplicate value to be stored. Vector, ArrayList and LinkedList classes provide implementation of List interface. Functionality of a List is represented by List interface which extends Collection interface and adds following methods-

SN Method Description
1 add() Is use to insert to an element to a list. public boolean add(int index, Object element)
2 addAll() Is use to insert all the elements of given Collection to a Lsit. public boolean addAll(int index, Collection c)
3 get() Is use to obtain an element from specified index the List. public Object get(int index)
4 set() Is used to replaces an element at the specified position in the List. public Object set(int index, Object o)
5 remove() Is used to remove element stored at the given position in a list. public boolean remove(int index)Is used to replaces an element at the specified position in the List. public Object set(int index, Object o)
6 indexOf() Is usedto find out the first occurrence position of the given element in the list. public int indexOf(Object element)
7 lastIndexOf() Is usedto find out the last occurrence position of the given element in the list. public int lastIndexOf(Object element)
8 listIterator() Is used to obtain the ListIterator for traversing the element of the list. public ListIterator listIterator() public ListIterator listIterator(int index)

Java ListIterator Interface

ListIterator is an extension of Iterator, which provide the facility of by directional traversing of List element. ListIterator extends Iterator and only available in List Collection.

Methods of ListIterator Interface

It contain all the Iterator methods and adds following methods

SN Method Description
1 hasPrevious() Is used to find out whether the element behind the current position or not public boolean hasPrevious()
2 previous() Is used to obtain previous element.public object - previous()
3 add() Is used to add an element at the current postion in the List. public void add(Object e)
4 set() Is used to replace an element at the current position in the list. public void set(Object e)

Difference between Iterator and ListIterator

SN Iterator ListIterator
1 Iterator is an interface of Collection framework which provide functionality of traversing the element of all Collection in only forward direction. ListIterator is an extension of Iterator, which provide the facility of by directional traversing of List element.
2 Each Collection provides its own implementation of Iterator. ListIterator extends Iterator and only available in List Collection.

Methods of Iterator Interface.

  1. hasNext() : This method returns true if the Iterator has more element for traversing.
  2. public boolean hasNext()
  3. next() : Use to obtain the next element from the Iterator.
  4. public object next()
  5. remove() : Use to remove last traverse element from the Collection.
  6. public void remove()traverse element from the Collection.
  7. public void remove()

Methods of ListIterator Interface

It contain all the Iterator methods and adds following methods-

  1. hasPrevious() : Is used to find out whether the element behind the current position or not.
  2. public boolean hasPrevious()
  3. previous() : Is used to obtain previous element.
  4. public object - previous() 3- add() : Is used to add an element at the current postion in the List.
  5. public void add(Object e)

  6. set() : Is used to replace an element at the current position in the list.
  7. public void set(Object e)