Collections Framework - The List Interface

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

List Interface Overview

  • List interface is a child interface of the Collection interface.
  • If we want to represent a group of individual objects where duplicates are allowed and insertion order preserved then we should go for List.
  • We can preserve insertion order and we can differentiate duplicate objects by using Index hence index will play a very important role in List.
  • The user of this interface has precise control over where in the list each element is inserted.
  • A list may contain multiple null elements.
  • List interface can contain duplicate elements. 
List interface implementations classes:

List Interface with It's ArrayList Implementation Class Example

Here is a simple List interface example using the ArrayList implementation class. This example demonstrates that List allows to store duplicate elements, null values, and maintains the insertion order:

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 allows you to have ‘null’ elements.

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


        // access elements from list


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

List Interface with It's LinkedList Implementation Class Example

The following example shows how to create a LinkedList and add new elements to it. 
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.
        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
        System.out.println("After addFirst(\"Strawberry\") : " + fruits);

        // Adding an element at the end of the LinkedList 
        // (This method is equivalent to the add() method)
        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<>();

        System.out.println("After addAll(moreFruits) : " + fruits);
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

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

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, it's methods, and usage with examples.

Related Guides

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course