List Interface - Notes By ShariqSP

List Interface in Java

The List interface in Java represents an ordered collection of elements. It extends the Collection interface and provides methods to access elements by their index.

Methods:

  • add(E element): Adds the specified element to the end of the list.
  • add(int index, E element): Inserts the specified element at the specified position in the list.
  • remove(int index): Removes the element at the specified position in the list.
  • get(int index): Returns the element at the specified position in the list.
  • size(): Returns the number of elements in the list.
  • isEmpty(): Returns true if the list contains no elements.
  • contains(Object obj): Returns true if the list contains the specified element.
  • clear(): Removes all elements from the list.
  • indexOf(Object obj): Returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain the element.
  • lastIndexOf(Object obj): Returns the index of the last occurrence of the specified element in the list, or -1 if the list does not contain the element.
  • addAll(Collection collection): Adds all of the elements in the specified collection to the end of the list.
  • addAll(int index, Collection collection): Adds all of the elements in the specified collection at the specified position in the list.
  • remove(Object obj): Removes the first occurrence of the specified element from the list, if it is present.
  • removeAll(Collection collection): Removes all of the elements in the specified collection from the list, if they are present.
  • retainAll(Collection collection): Retains only the elements in the list that are contained in the specified collection.
  • subList(int fromIndex, int toIndex): Returns a view of the portion of the list between the specified fromIndex, inclusive, and toIndex, exclusive.

Implementation Classes:

Some of the commonly used implementation classes of the List interface in Java are:

  • ArrayList: Resizable array implementation of the List interface.
  • LinkedList: Doubly linked list implementation of the List interface.
  • Vector: Synchronized resizable array implementation of the List interface.