List Interface in Java
The List interface provides a way to store the ordered collection. It is an
ordered collection of objects in which duplicate values can be stored. Since
List preserves the insertion order, it allows positional access and insertion of
elements.
Declaration: The List interface is declared as:
public abstract interface List extends Collection
The List interface is used by one of the most popular classes in Java
collections which is ArrayList. It is also implemented by the LinkedList class.
It is also implemented by the Vector class and it is also used by the Stack
class which is inherited from the Vector class. These are the 4 main classes
in Java collections that implement the List interface.
Now, List interface inherits Collection interface, so it has all the methods
available in the Collection interface and along with them it has some
additional methods specific to List Interface only.
Example of a List:
Java
// Java program to demonstrate a List
import java.util.*;
public class ListDemo {
public static void main(String[] args)
{
// Creating a list
List<Integer> l1
= new ArrayList<Integer>();
// Adds 1 at 0 index
l1.add(0, 1);
// Adds 2 at 1 index
l1.add(1, 2);
System.out.println(l1);
// Creating another list
List<Integer> l2
= new ArrayList<Integer>();
l2.add(1);
l2.add(2);
l2.add(3);
// Will add list l2 from 1 index
l1.addAll(1, l2);
System.out.println(l1);
// Removes element from index 1
l1.remove(1);
System.out.println(l1);
// Prints element at index 3
System.out.println(l1.get(3));
// Replace 0th element with 5
l1.set(0, 5);
System.out.println(l1);
}
}
Output:
[1, 2]
[1, 1, 2, 3, 2]
[1, 2, 3, 2]
2
[5, 2, 3, 2]
The additional methods available in the List interface are:
Method Description
get(int index) This method returns element at the specified index.
This method replaces element at given index with new element. This
set(int index, E function returns the element which was just replaced by new
element) element. Since it is a generic function, so "E" here is denotes the
type of element in the List.
This method returns the first occurrence of the given element or -1 if
indexOf(element)
the element is not present in the list.
List interface has an enhanced version of the iterator. The iterator in
Collection interface allows us to traverse only in the forward
listIterator()
direction, where as a List iterator is an ehnaced iterator and it
allows us to traverse in both forward and backward directions.
listIterator(int This function returns an iterator pointing to the element at index
index) "index".
This method removes an element from the specified index. It shifts
remove(int index)
subsequent elements(if any) to left and decreases their indexes by 1.
This method is used to remove the first occurrence of the given
remove(element)
element in the list.
We will discuss examples of these functions in details in the Methods in
ArrayList.
Advantages:
We have a common interface present in the List interface. The above
methods can be called on LinkedList, Vectors or ArrayList.
There are common implementations of above methods in the
AbstractList class which is used by LinkedList and ArrayList.