Java List Interface

Category: Java   Tags: Java, Java Collection, Java List Interface

The List interface extends Collection and stores a sequence of elements. A list may contain duplicate elements. Elements can be inserted or accessed by their position in the list. List is a generic interface:

                          interface List<E>
                                

Here, E specifies the type of objects that the list will contain.

Methods

In addition to the methods defined by Collection, List defines some of its own:

void add(int index, E element)
Inserts the specified element at the specified position index in this list. Any element currently at that position or any subsequent elements are shifted up.
E get(int index)
Returns the object stored at the specified index within the invoking list.
ListIterator<E> listIterator()
It returns an iterator that will do the iteration from the starting position of the invoking list.
ListIterator<E> listIterator(int index)
Returns an iterator to the invoking list that begins at the specified index.
int indexOf(Object element)
Returns the index of the first instance of element in the invoking list. If element doesn't exist,it will return -1.
int lastIndexOf(Object element)
Returns the index of the last instance of element in the invoking list. If element doesn't exist,it will return -1.
E remove(int index)
Removes the element at position index from the invoking list and returns the deleted element.
E set(int index, E element)
Assigns element to the location specified by index within the invoking list and returns the old value.
List<E> subList(int start, int end)
Returns a list that includes elements from start to end-1 in the invoking list.
void sort(Comparator<? super E> comparator)
Sorts the list using the comparator comparator.