List Interface

list interface edited

First thing to mention about List interface is , List is a child interface to Collection interface. List is an ordered collection which is introduced in 1.2v of Java . List interface is also known to be Sequence collection. The hierarchy of List interface is shown below 

public Interface List<>

All Super Interfaces ( extends)

 Collection , Iterable

All known implementing classes

AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector

list interface heirarchy

List Interface

To represent a group of individual objects or elements as a single entity thenone should use collections. A collection can be a List , Set or Queue . If the collection is a List i.e

List<int> list = new ArrayList<int>();
//creates a ArrayList collection with name 'list' with default size 10 and can 
be increased if the user inserts more elements than 10.
  • List has two general purpose implementation classes ArrayList and LinkedList and two legacy classes one is Vector and Stack.
  • In order to create a collection in List , we have to use one of the general purpose implementations classes or legacy classes.
  • The term legacy classes means , Vector and Stack are introduced in the old version of Java i.e 1.0v.
  • List is an ordered collection , i.e in which order the user inserts the elements , the same order will be maintained in the list collection.
  • The user can access the elements with the use of index number of the element.
  • List allows duplicate elements and null elements.
  • In addition to the Collection interface methods , List interface also provides some additional methods for various purposes which are applicable only to list collections.

List Interface Methods

Apart from Collection interface methods like add or remove , List interface also provides methods only for List interface implementation classes. If you’re not familiar with Collection interface methods , please do follow read here ( Collection interface methods). The following operations can be done with List interface methods.

  1. Positional Access : Positional access is known to be accessing and manipulating the elements based on their numerical position on the list  (i.e) by knowing the index position of the element , programmer can manipulate the elements on that index. This includes methods like set , get ,add , addAll  and remove
  2. Search Operations : Searches for a specified object (element ) in the list and returns the index value of that specified object , which can be done using indexOf , lastIndexOf. 
  3. Iterator : Apart from  one direction iterator method (iterator method is used to traverse the elements one by one in a collection which is only one direction ) List interface provides a rich bi-directional  method for traversing the elements  known as listIterator method which is taken from ListInterator interface.
  4. Range-view operation : range-view operation is know to be returning a portion of elements in a list by specifying fromIndex to toIndex , this method is called subList. which returns a list  between the specified indexes.

Please do read every method mentioned below , they are explained in list perspective.

Index Methods
1 add(Object O)

public boolean add(Object O)

  • This method adds the specified element to the end of the list.
  • In other words appends the specified element to the end of the list.
  • Returns “true” if the element is added successfully.
  • Returns “false” if the list does not allow duplicate elements or the element is already existed.
    • Some lists does not allow to add “null” and some kind of lists have some restrictions on the type of element adding.

Specified by

add in interface Collection.

Exceptions

Throws

  1. UnsupportedOperationException – if the add operation is not supported by this list
  2. ClassCastException : if the class of the specified element prevents it from being added to the list.
  3. NullPointerException : If the specified element is “null” and the list does not allow to add null elements
  4. IllegalArgumentException : Suppose the specified element is ‘String’ type but the list allows only ‘int’ type elements , then this exception occurs.

 2 add(int index ,E element )

public boolean add(int index , E element)

  • This method inserts the specified element at the specified index.
  • If there is an element already existed at the position , then that element will shift to the next index and subsequent elements to the right.
  • Returns “true” if the element is added successfully.
  • Returns “false” if the list does not allow duplicate elements or the element is already existed.
    • Some lists does not allow to add “null” and some kind of lists have some restrictions on the type of element adding.

 

Exceptions

Throws

  1. UnsupportedOperationException – if the add operation is not supported by this list
  2. ClassCastException : if the class of the specified element prevents it from being added to the list.
  3. NullPointerException : If the specified element is “null” and the list does not allow to add null elements
  4. IllegalArgumentException : Suppose the specified element is ‘String’ type but the list allows only ‘int’ type elements , then this exception occurs.
  5. IndexOutOfBoundException : If the index is out or range index value should be (index <0 || index >=size())

 3  addAll(int index ,Collection c )

public boolean addAll(int index , collection c)

Inserts all of the elements present in the specified collection ‘c’  into the list at the specified position. If there is an element already existed at that position then that element will shift to the right and so on all the subsequent elements  shifts to the right by increasing the size of the list.

  • Returns “true” if the element is added successfully.
  • Returns “false” if the list does not allow duplicate elements or the element is already existed.
    • Some lists does not allow to add “null” and some kind of lists have some restrictions on the type of element adding.

 

Exceptions

Throws

  1. UnsupportedOperationException – if the addAll operation is not supported by this list
  2. ClassCastException : if the class of the specified collection  prevents it from being added to the list.
  3. NullPointerException : If the specified collection ‘c’ contains any  “null” elements and the list does not allow to add null elements (OR) the specified collection itself is null then this exception will occur.
  4. IllegalArgumentException : Suppose the specified collection is ‘String’ type but the list allows only ‘int’ type elements , then this exception occurs.
  5. IndexOutOfBoundException : If the index is out or range (index <0 || index >=size())

 4  get(int index)

public int get(int index)

  • This method returns the element at the specified position in the list.
  • If the programmer wants to know which element is currently located at that index then this method will be very handy.

Parameters

index : index of the element to be returned

Exceptions

IndexOutOfBoundException : If the specified index is out of range , this exception will raise .  (index<0 || index >=size()

 5  set(int index , Element e)

public int set(int index , Element e)

  • Replaces the specified element in the specified index of the list.
  • And returns the old element ( before replacing).

parameters :

index : index of the element to replace

element : element to be stored at the specified index.

Exceptions

Throws

  1. UnsupportedOperationException – if the set  operation is not supported by this list
  2. ClassCastException : if the class of the specified collection  prevents it from being added to the list.
  3. NullPointerException : If the specified element is “null”  and the list does not allow to add null elements .
  4. IllegalArgumentException : Suppose the specified element is ‘String’ type but the list allows only ‘int’ type elements , then this exception occurs.
  5. IndexOutOfBoundException : If the index is out or range (index <0 || index >=size())

 6  indexOf(Object o)

public int indexOf(Object o)

  • Since list allow duplicates elements , one element can be present more than once in the list. Suppose in a list there is an element called ‘5’ which is present 5 times in different indexes.
  • This method returns the value of index of the first occurrence of the specified element in the list.
  • Returns -1 if there is no specified element in the list.
  • Object ‘o’ is the element to be search.

Exceptions

  1. ClassCastException : If the type of the specified element is incompatible with the list. 
  2. NullPointerException : If the specified element is “null” and the list does not allow  null elements.

 7  lastIndexOf(Object o)

public int lastIndexOf(Object o)

  • Since list allow duplicates elements , one element can be present more than once in the list. Suppose in a list there is an element called ‘5’ which is present 5 times in different indexes
  • This method returns the value of index of the last occurrence of the specified element in the list.
  • Returns -1 if there is no specified element in the list.
  • Object ‘o’ is the element to be search.

Exceptions

  1. ClassCastException : If the type of the specified element is incompatable with the list. 
  2. NullPointerException : If the specified element is “null” and the list does not allow  null elements

 8  remove(Object o)

public E remove(int index)

  • Removes an element at the specified index in the list.
  • All the subsequent elements(elements after the specified index) will shift to the left (i.e)  subtracts one from their indices).
  • This method has the return type Element , which returns the removed element
  • This method is identical to Collection interface remove method but does not returns the removed element.

Exceptions

  1. UnSupportedOperationException : If the remove operation is not supported by this list. 
  2. IndexOutOfBoundException : If the specified index is out of range 

(index<0 || index>=size())

 9 listIterator()

public ListIterator<> listIterator()

  • It is  other kind of iterator method which is available only to list implementations.
  • ListIterator is an interface which provides two main methods to List ,  listIterator() and listIterator(int index).
  • ListIterator interface is the child interface to Iterator interface.
  • listIterator() method returns the elements one by one over a list in a proper sequence.
  • The returned list is fail-fast.

10 listIterator(int index)

public ListIterator<> listIterator(int index)

  • It is  other kind of iterator method which is available only to list implementations.
  • ListIterator is an interface which provides two main methods to List ,  listIterator() and listIterator(int index).
  • ListIterator interface is the child interface to Iterator interface.
  • listIterator(int index) method returns the elements one by one over a list from a specified position.

For Example :

listIterator(int 10)
  • Suppose in list there are 0 to 19 elements , i.e the indexes will be 20 (because index starts from 0). If the specified index in a list is ’10’ then the first element to be returned by an initial call to next() will be 10 , 11 , 12 and so on up to 19.
  • If the initial call is previous() then it would return the element with the specified index minus one i.e ‘9’.

Exceptions

IndexOutOfBoundException : If the index is out of range (index<0||index>size())

11  subList(int fromIndex , int toIndex)

List<E> subList(int fromIndex , int toIndex)

  • This method returns a portion of elements between the specified indexes over a list.
  • Returns the elements between inclusive the fromIndex  and exclusive toIndex.
  • If the fromIndex and toIndex are same , then the returned list is empty.
  • The returned list supports all the list operations to be operated.
  • Any operation that expects a portion of the list over a list can be used with this range operation

For Example , this statement removes a range of elements from a list

list.subList(fromIndex , toIndex).clear();

Parameters

fromIndex : low end point inclusive of the subList

toIndex : high end point exclusive of the subList

Returns

A view of the specified range within the list.

Exceptions

IndexOutOfBoundException : For an illegal end point index value

(fromIndex<0 || toIndex>size || fromIndex > toIndex)

2 thoughts on “List Interface

  1. Thank you for the information , I have been searching for the right material on Java’s collection framework. Great website 100 likes

Leave a Reply

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