List Interface Tutorial In Java With Example

List is the sequence of elements, or we can say collection of elements, on which user has precise control over where an elements are inserted. These elements can be accessed by their index value (Starting from 0) which is of Integer type and with the help of this index, user can search any element easily.

Important Note: List allows duplicate values as well as null values in it.

List is a child of Collection Interface, In addition to adding all methods implemented in Collection Interface, List adds its own methods to add or delete elements on the bases of index that are discussed below.

Some of these methods can throw exceptions, (like UnsupportedOperationException) if the given collection cannot be modified and Exception ClassCastException is generated if given object is not compatible with other.


Hierarchy of List Interface

List Interface extends Collection Interface which further extends Iterable Interface, as shown in figure 1.

hierarchy of List Interface


List Implementations:

ArrayList 

ArrayList is a Class that is implementation of List Interface, that allows elements to be added and removed dynamically from it. If in case more elements are added than its capacity, Arraylist automatically increases its size. Elements in it can be accessed directly using get() and set() methods using index value, but add() and remove() operations has very low performance as all the elements are shifted by this operation leading to change in index values.

LinkedList

LinkedList is a class that is also the implementation of List Interface which is implemented using Doubly Linked List, It allows elements to be added dynamically in it, if in case more elements are added than its capacity, linkedlist  automatically increases its size. Operations such as add() and remove() performs better than arrayList whereas operations such as get() and set() performs very poor as compared to arrayList.

Vectors

Vector is also the implementation of List Interface. Points that distinguishes vector from arraylist and linkedlist are vectors are Synchronized and also vector is a legacy class that is, it is introduced before JDK 1.2. and it do not implement most of the collection methods.


Example of List Interface In JAVA:

Let us discuss List Interface with the help of program. In this program objects of all the List implementations (Vector, ArrayList, LinkedList) are created and data of String type is added and then displayed, we have divided this program into 3 Steps that we will discuss one by one.

import java.util.*;
public class ListInterfaceDemo{
public static void main(String args[]){

//Step 1: Create Objects
List<String> arrayList = new ArrayList<String>(); 
List<String> linkedList= new LinkedList<String>();
List<String> vectorObject= new Vector<String>();

//Step 2: Add values
arrayList.add("This");                         
arrayList.add("is");
arrayList.add("ArrayList");

linkedList.add("This");
linkedList.add("is");
linkedList.add("LinkedList");

vectorObject.add("This");
vectorObject.add("is");
vectorObject.add("vector");

//Step 3:Display values
System.out.println("ArrayList: "+arrayList);
System.out.println("LinkedList: "+linkedList);
System.out.println("Vector : "+vectorObject);
}
}

Output:

ArrayList: [This, is, ArrayList]
LinkedList: [This, is, LinkedList]
Vector : [This, is, vector]

Description:

  • In Step 1, we have created objects of ArrayList, LinkedList and Vector class, we have defined these objects to store value of String type.
  • In Step 2, we have used add method to store values in the data structures that we have created in step 1.
  • In Step 3, we have displayed values of these objects.

Important Points Of List Interface:

  • List has three implementation classes Vector, LinkedList and ArrayList. If we have more requirement of just retrieving and setting the elements than ArrayList should be preferred, or if we have more requirement of adding and removing the elements (or we can say modification) than LinkedList should be preferred.
  • List allows duplicate values in it.
  • List allows null values to be stored.
  • Some of the methods throw UnsupportedOperationException if the given collection cannot be modified.
  • Some of the methods throw ClassCastException if the given object is not compatible with other.

Importance of List Interface:

  • As List is dynamic in nature, it grows and shrinks according to the requirement. So it is very helpful when we don’t know capacity of our data structure in advance, we can just declare a List with some initial capacity that automatically increases or decreases its size when elements are added or removed respectively.
  • Similar to Arrays we have various methods to access elements very smoothly using index, we have three implementations of List Interface that are Vectors, LinkedList and ArrayList, which can be used according to the requirement.

List Quick Revision Points:

  • Size increases dynamically.
  • Duplicate values are allowed.
  • Null value is allowed.
  • Arraylist is preferred when getting and setting elements is required in access.
  • LinkedList is preferred when modifications are required in access.
  • Vector is a legacy class that is, it is introduced before JDK 1.2.

List Methods:

1. void add(int index, Object obj)

This method adds specified element at the specified index, moving the entire elements one step up, avoiding any type of overlapping of element.

2. boolean addAll(int index, Collection c)

This method adds all  elements of Collection c after the specified index, moving the entire elements up, avoiding any type of overlapping, This method returns true if the operation is successful or else false.

3. Object get(int index)

This method retrieves element at the specified index.

4. int indexOf(Object obj)

This method retrieves the first occurrence of the given element in the List or it will return -1 if there is no such elements.

5. int lastIndexOf(Object obj)

This method retrieves the last occurrence of the given element in the List or it will return -1 if there is no such elements.

6. ListIterator listIterator( )

This method gives object of ListIterator from the index 0 of the list, to traverse all the elements from the start.

7. ListIterator listIterator(int index)

This method gives object of ListIterator starting from the specified index of the list to traverse all the elements from the start of that index.

8. Object remove(int index)

This method removes an element from the list at specified index, In addition to removing this method also returns removed element. After deleting that element indexes of subsequent elements in the list are decreased by 1.

9. Object set(int index, Object obj)

This method sets the element at the specified index, if there is already any element present than that element is replaced by the new one.

10. List subList(int start, int end)

This method returns the sub-list from the index “start” to index “end”.

Leave a Reply

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