Java Collections Framework - The List Interface

In this guide, we will learn about the List interface of the Java Collections framework with an example.

Important Key Points About List Interface

Here are some key points about the List Interface in Java: 

Ordered Collection: 

The List interface extends the Collection interface and represents an ordered collection (also known as a sequence). The elements in a List can be inserted or accessed at any position based on the index.

Duplicates: 

The List allows duplicates. It means you can insert duplicate elements in the List. 

Null Elements:

The List allows any number of null elements. You can have a List with all elements as null. 

Methods: 

In addition to the methods inherited from the Collection interface, the List interface includes methods for position (index-based) access, search operations, and list iteration. 

Subinterfaces and Implementations: 

The commonly used classes that implement the List interface are ArrayListLinkedListVector, and Stack.

ListIterator: 

The List interface provides a special iterator, called ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. 

Mutability: 

The List interface supports elements insertion and removal, and it is typically more flexible than arrays.

Use-cases: 

The List is a good choice if you need to maintain the insertion order, allow duplicates and nulls, and frequently access elements with the index. 

Equality: 

Two List objects are considered equal if they contain the same elements in the same order.

Example 1: List Interface with Its ArrayList Implementation Class

Here is a simple List interface example using the ArrayList implementation class. This example demonstrates that List allows storing duplicate elements, and null values, and maintains the insertion order:
package com.java.collections.interfaces;

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

public class ListDemo {
    public static void main(String[] args) {
        List < String > list = new ArrayList < > ();
        // List allows you to add duplicate elements
        list.add("element1");
        list.add("element1");
        list.add("element2");
        list.add("element2");
        System.out.println(list);

        // List allows you to have ‘null’ elements.
        list.add(null);
        list.add(null);
        System.out.println(list);

        // insertion order
        list.add("element1"); // 0
        list.add("element2"); // 1
        list.add("element4"); // 2
        list.add("element3"); // 3
        list.add("element5"); // 4

        System.out.println(list);

        // access elements from list

        System.out.println(list.get(0));
        System.out.println(list.get(4));

    }
}
Output
[element1, element1, element2, element2]
[element1, element1, element2, element2, null, null]
[element1, element1, element2, element2, null, null, element1, element2, element4, element3, element5]
element1
null

Example 2: List Interface with Its LinkedList Implementation Class

The following example shows how to use the List interface with LinkedList implementation class:
package com.javaguides.collections.linkedlistexamples;

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

public class CreateLinkedListExample {
    public static void main(String[] args) {
        // Creating a LinkedList
        LinkedList<String> fruits = new LinkedList<>();
        
        // Adding new elements to the end of the LinkedList using add() method.
        fruits.add("Banana");
        fruits.add("Apple");
        fruits.add("mango");
        
        System.out.println("Initial LinkedList : " + fruits);

        // Adding an element at the specified position in the LinkedList
        fruits.add(2, "Watermelon");
     
        System.out.println("After add(2, \"D\") : " + fruits);

        // Adding an element at the beginning of the LinkedList
        fruits.addFirst("Strawberry");
        System.out.println("After addFirst(\"Strawberry\") : " + fruits);

        // Adding an element at the end of the LinkedList 
        // (This method is equivalent to the add() method)
        fruits.addLast("Orange");
        System.out.println("After addLast(\"F\") : " + fruits);

        // Adding all the elements from an existing collection to 
        // the end of the LinkedList
        List<String> moreFruits = new ArrayList<>();
        moreFruits.add("Grapes");
        moreFruits.add("Pyrus");

        fruits.addAll(moreFruits);
        System.out.println("After addAll(moreFruits) : " + fruits);
    }
}
Output:
Initial LinkedList : [Banana, Apple, mango]
After add(2, "D") : [Banana, Apple, Watermelon, mango]
After addFirst("Strawberry") : [Strawberry, Banana, Apple, Watermelon, mango]
After addLast("F") : [Strawberry, Banana, Apple, Watermelon, mango, Orange]
After addAll(moreFruits) : [Strawberry, Banana, Apple, Watermelon, mango, Orange, Grapes, Pyrus]

The List Interface Class Diagram

List Interface Methods

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

List Interface Common Implementation

List interface implementations classes:

What's next?

In this guide, we have learned all about the List interface, its methods, and its usage with examples.

In the next guide, we will learn about the ArrayList class of the Java collections framework.

Related Guides

Comments