November 26, 2015

List Interface

A list is an ordered collection sometimes is also known as sequence. List may have contains duplicate values.
AbstractList is an abstract class which implements List interface. ArrayList, LinkedList, Vector are the concrete sub class of AbstractList.

As List interface implements the root interface (which is Collection) it has all the methods of Collection interface but also some new methods are there which is listed below:


  1. void add(int index, Object data)
  2. void set(int index, Object data)
  3. Object get(int index)
  4. Object remove(int index)
  5. int indexOf(Object data)
  6. int lastIndexOf(Object data)
  7. List subList(int start, int end)
  8. ListIterator listIterator()

Now there is a method listIterator() which return the reference of ListIterator. We can also use the simple iterator() of the Collection interface. But the ListIterator have some of the extra facility provided which is not in the Iterator interface.

ListIterator Interface

ListIterator is an interface which extends from Iterator. Using the ListIterator we can also add, set elements at the specified position which is not possible in the Iterator interface.
We can also get an index of the next elements using ListIterator and also can get the previous element while iterating in forward direction.
We can also make iterations in backward directions, which is also not possible in Iterator interface.
Following is the list of the methods provided by ListIterator interface :

  1. hasNext()
  2. next()
  3. remove()
  4. previous()
  5. preivousIndex()
  6. nextIndex()
  7. add()
  8. set()

No comments:

Post a Comment