List Interface in Java

List Interface in Java

The List interface in Java provides a way to store an ordered collection of objects, including duplicate values. Since List preserves the insertion order, it allows positional access and insertion of elements. The List interface is declared as an abstract interface that extends the Collection interface.

One of the most popular classes in Java collections that implements the List interface is ArrayList. LinkedList class also implements the List interface. Vector class and Stack class, which is inherited from Vector class, also implement the List interface. These are the four main classes in Java collections that implement the List interface.

Since List interface is inherited by the Collection interface, it has all the methods available in the Collection interface along with some additional methods specific to the List interface only.

Here is an example of how to use the List interface in Java:

java
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 List interface provides additional methods specific to the List interface only. These methods include:

  • get(int index): This method returns the element at the specified index.
  • set(int index, E element): This method replaces the element at the given index with a new element. This function returns the element that was just replaced by the new element. Since it is a generic function, "E" denotes the type of element in the List.
  • indexOf(element): This method returns the first occurrence of the given element or -1 if the element is not present in the list.
  • listIterator(): The List interface has an enhanced version of the iterator. The iterator in the Collection interface allows us to traverse only in the forward direction, whereas a List iterator is an enhanced iterator and allows us to traverse in both forward and backward directions.
  • listIterator(int index): This function returns an iterator pointing to the element at index "index".
  • remove(int index): This method removes an element from the specified index. It shifts subsequent elements(if any) to the left and decreases their indexes by 1.
  • remove(element): This method is used to remove the first occurrence of the given element in the list.

The List interface provides a common interface for ArrayList, LinkedList, and Vector, making it easier to work with these classes interchangeably. Additionally, there are common implementations of the above methods in the AbstractList class, which is used by LinkedList.

Previous Post Next Post