List Interface

List(I)

  • List interface is the child interface of collection interface.
  • List is index based and It is able to store all the elements as per indexing.
  • If you want to represent a group of individual Objects as a single entity where duplicates are allowed and insertion order must be preserved then we should go for List.
  • We can preserve insertion order via index and we can differentiate duplicates objects by using index, hence index play very important role in List.
  • List is able to allow any number of null elments.
  • List allows heterogenous elements.
#

In this image you saw above class implements interface and interface extends interface, for example 

ArrayList implements List and TreeSet implements NavigableSet extends SortedSet extends Set extends Collection.

Code Example
Output
List elements: [apple, 10, null, banana, 10, 3.14, null]
Element at index 0: apple
Element at index 2: null
List after modification: [apple, orange, null, banana, 10, 3.14, null]
List after removing element at index 3: [apple, orange, null, 10, 3.14, null]
Iterating over the list:
Element at index 0: apple
Element at index 1: orange
Element at index 2: null
Element at index 3: 10
Element at index 4: 3.14
Element at index 5: null
List contains 'apple': true
Index of first null element: 2

List interface methods

The List interface in Java extends the Collection interface and includes additional methods specifically for manipulating elements based on their positions (indices) in the list. Here are the methods that are specific to the List interface, excluding those inherited from the Collection interface:

Method SignatureDescription
void add(int index, E element)  Inserts the specified element at the specified position in the list.
boolean addAll(int index, Collection<? extends E> c)Inserts all elements from the specified collection at the specified position.
E get(int index)    Returns the element at the specified position in the list.
 int indexOf(Object o)    Returns the index of the first occurrence of the specified element.
int lastIndexOf(Object o)Returns the index of the last occurrence of the specified element.
ListIterator<E> listIterator()Returns a list iterator over the elements in the list.
ListIterator<E> listIterator(int index)Returns a list iterator starting at the specified position.
E remove(int index)    Removes the element at the specified position in the list.
E set(int index, E element)Replaces the element at the specified position with the specified element.
List<E> subList(int fromIndex, int toIndex)Returns a view of the specified range within the list.
int lastIndexOf(Object o)

Returns the index of the last occurrence of the specified element in the list, or -1 if the list does not contain the element.

ListIterator<E> listIterator()

Returns a list iterator over the elements in the list (in proper sequence).

ListIterator<E> listIterator(int index)

Returns a list iterator over the elements in the list (in proper sequence), starting at the specified position in the list.

E remove(int index)

Removes the element at the specified position in the list and returns it.

E get(int index)

Returns the element at the specified position in the list.

E set(int index, E element)

Replaces the element at the specified position in the list with the specified element.

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

Returns a view of the portion of the list between the specified fromIndex, inclusive, and toIndex, exclusive.

void add(int index, E element)

Inserts the specified element at the specified position in the list.

Output
[B, A]
boolean addAll(int index, Collection<? extends E> c)

Inserts all of the elements in the specified collection into the list at the specified position.

int indexOf(Object o)

Returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain the element.