Collections Framework - The List Interface

Overview of List Interface

  • Elements of the lists are ordered using a Zero-based index.
  • You can access the elements of lists using an integer index - get() API
  • It is an ordered collection (also known as a sequence). It guarantees insertion order.
  • The user of this interface has precise control over where in the list each element is inserted.
  • It guarantees insertion order so a user can access elements by their integer index (position in the list), and search for elements in the list.
  • A list may contain multiple null elements.
  • List interface can contain duplicate elements. 
The Java platform contains two general-purpose List implementations - ArrayList and LinkedList.
Here is simple List interface example using ArrayList implementation class.

List Interface Simple Example

import java.util.ArrayList;
import java.util.List;

public class CreateArrayListExample {

    public static void main(String[] args) {
        // Creating an ArrayList of String using
     List<String> animals = new ArrayList<>();
        // Adding new elements to the ArrayList
[Lion, Tiger, Cat, Dog]

List Interface Hierarchy Diagram

From above diagram, the List interface extends Collection interface. So, All methods of Collection interface are inherited to List interface. Collection interface extends Iterable interface.

List Interface APIs/Methods

This class diagram shows a list of APIs/Methods that the List interface provides.

List Interface Implementations

The Java platform contains two general-purpose List implementations.
  • ArrayList , which is usually the better-performing implementation.
  • LinkedList , which offers better performance under certain circumstances.
We will learn List interface with these implementation class with examples. There separate post for each List Interface implementations and you will learn below topics in each implementation class.

ArrayList Implementation Class

  1. List interface Overview
  2. ArrayList class Overview
  3. Demonstrate with an example how the List contains duplicates and null values?
  4. List positional access and search operations
  5. How to search elements in List?
  6. How to perform range view operations in List?
  7. How to perform bulk operations using List?
  8. Iterating over an ArrayList
  9. Traverse the list in either direction using ListIterator interface
  10. Creating an ArrayList from another collection
  11. Removing elements from an ArrayList
Read ArrayList Class on Guide to ArrayList Class

LinkedList Implementation Class

  1. Overview of LinkedList class
  2. Add elements LinkedList API's with Examples
  • add(String element)
  • add(int index, String element)
  • addFirst(String e)
  • addLast(String e)
  • addAll(Collection<? extends String> c)
  1. Remove elements LinkedList API's with Examples
  • removeFirst()
  • removeLast()
  • remove(Object o)
  • removeIf(Predicate<? super String> filter)
  • clear()
  1. Retrieve elements LinkedList API's with Examples
  • getFirst()
  • getLast()
  • get(int index)
  1. Search elements LinkedList API's with Examples
  • contains(Object o)
  • indexOf(Object o)
  • lastIndexOf(Object o)
  1. Iterate LinkedList using Java 8 forEach() method
  • forEach(Consumer<? super String> action)
  • Iterator java.util.AbstractSequentialList.iterator()
  1. Difference between ArrayList and LinkedList
Read LinkedList Class on Guide to LinkedList Class