Java List

Java List Interface

Java List Interface

A List only cares about the index, you can not do anything without index in this data structure. Whether you want to add element, remove or access particular object from the list. List order the elements using Zero based index which you determine either by setting an element at particular position or by adding the element without any position specification.

The List interface represents an index based ordered collection of objects. This interface provides rich set of methods to manipulate the collection of objects.

The ArrayList, Vector and LinkedList are some of the examples of list which implements the List interface.


Some of the properties of List interface

  1. A List can contain one or more duplicate elements.
  2. A List can also contain many null objects.
  3. Objects in the lists are ordered using Zero based index.
  4. Elements can be accessed via integer index value.
  5. Objects can be inserted at any position by using index value.
  6. Elements can be removed from a specific position.
  7. List interface provides additional methods for positional access to list elements.
  8. List interface also provides special iterator called “ListIterator” for iterating the elements in the list.

Java List Interface Hierarchy

The List interface extends the Collection interface which then extends the root interface Iterable. Below is the interface hierarchy.

Java List Interface




Some Special Methods of List Interface


Method Return Signature Method Name and it’s description
boolean add(E e)
Add new element to the end of the list.
void add(int index, E element)
You can insert element at particular position.
boolean contains(Object o)
Method is used to check whether particular object exist, you need to override equals method in that case.
boolean equals(Object o)
Compares the two objects for equality, you need to override this method in your Class in order to get the proper result. By default equals uses “==” operator for comparison.
E get(int index)
Returns the element from the list at given index.
boolean isEmpty()
Method is used to check whether the list is empty or not.
Iterator<E> iterator()
Returns the object of iterator.
int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
ListIterator<E> listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
E remove(int index)
Remove the element at given position.
E set(int index, E element)
You can set the value of element at the particular position.
int size()
Returns the size of the list.
List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.


Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *