The List Interface in java

The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements. Elements can be inserted or accessed by their position in the list, using a zero-based index. A list may contain duplicate elements. List is a generic interface that has this declaration:

interface List<E>

Here, specifies the type of objects that the list will hold

Table of List interface

 Method Description
 boolean add(E obj)Adds obj to the invoking collection. Returns true if obj was added
   to the collection. Returns false if obj is already a member of the
   collection and the collection does not allow duplicates.
   
 boolean addAll(Collection<? extends E> c)Adds all the elements of c to the invoking collection. Returns true
   if the operation succeeded (i.e., the elements were added).
   Otherwise, returns false.
 void clear( ) Removes all elements from the invoking collection.
   
 boolean contains(Object obj)Returns true if obj is an element of the invoking collection.
   Otherwise, returns false.
   
 boolean containsAll(Collection<?> c)Returns true if the invoking collection contains all elements
   of c. Otherwise, returns false.
   
 boolean equals(Object obj)Returns true if the invoking collection and obj are equal.
   Otherwise, returns false.
   
 int hashCode( )Returns the hash code for the invoking collection.
   
 boolean isEmpty( )Returns true if the invoking collection is empty. Otherwise,
   returns false.
   
 Iterator<E> iterator( )Returns an iterator for the invoking collection.
   
 boolean remove(Object obj)Removes one instance of obj from the invoking collection. Returns
   true if the element was removed. Otherwise, returns false.
   
 boolean removeAll(Collection<?> c)Removes all elements of c from the invoking collection. Returns
   true if the collection changed (i.e., elements were removed).
   Otherwise, returns false.
   
 boolean retainAll(Collection<?> c)Removes all elements from the invoking collection except those
   in c. Returns true if the collection changed (i.e., elements were
   removed). Otherwise, returns false.
    
 int size( ) Returns the number of elements held in the invoking collection.
   
 Object[ ] toArray( )Returns an array that contains all the elements stored in the
   invoking collection. The array elements are copies of the
   collection elements.
   
 <T> T[ ] toArray(T array[ ])Returns an array that contains the elements of the invoking
   collection. The array elements are copies of the collection
   elements. If the size of array equals the number of elements,
   these are returned in array. If the size of array is less than the
   number of elements, a new array of the necessary size is allocated
   and returned. If the size of array is greater than the number of
   elements, the array element following the last collection element
   is set to null. An ArrayStoreException is thrown if any collection
   element has a type that is not a subtype of array.
    
The Methods Defined by Collection
Method Description 
void add(int index, E obj) Inserts obj into the invoking list at the index passed in index. 
  Any preexisting elements at or beyond the point of insertion 
  are shifted up. Thus, no elements are overwritten. 
    
boolean addAll(int index, Inserts all elements of c into the invoking list at the index 
Collection<? extends E> c) passed in index. Any preexisting elements at or beyond the 
  point of insertion are shifted up. Thus, no elements are 
  overwritten. Returns true if the invoking list changes and 
  returns false otherwise. 
    
E get(int index) Returns the object stored at the specified index within the 
  invoking collection. 
    
int indexOf(Object obj) Returns the index of the first instance of obj in the invoking 
  list. If obj is not an element of the list, –1 is returned. 
    
int lastIndexOf(Object obj) Returns the index of the last instance of obj in the invoking 
  list. If obj is not an element of the list, –1 is returned. 
    
ListIterator<E> listIterator( ) Returns an iterator to the start of the invoking list. 
    
ListIterator<E> listIterator(int index) Returns an iterator to the invoking list that begins at the 
  specified index. 
    
E remove(int index) Removes the element at position index from the invoking list 
  and returns the deleted element. The resulting list is compacted. 
  That is, the indexes of subsequent elements are decremented 
  by one. 
    
E set(int index, E obj) Assigns obj to the location specified by index within the 
  invoking list. 
    
List<E> subList(int start, int end) Returns a list that includes elements from start to end–1 in the 
  invoking list. Elements in the returned list are also referenced 
  by the invoking object. 
The Methods Defined by Collection

Leave a Comment