English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java Other Topics

Java Lists (Lists)

In this tutorial, we will learn about the List interface and its methods in Java.

In Java, the List interface is an ordered collection that allows us to store and access elements in order. It extends the Collection interface.

Classes that implement List

Since List is an interface, you cannot create an object from it.

To use the features of the List interface, we can use the following classes:

These classes are defined and implement the List interface in the Collections framework.

How to use List?

In Java, you must import the java.util.List package to use List.

//List's ArrayList implementation
List<String> list1 = new ArrayList<>();
// List's LinkedList implementation
List<String> list2 = new LinkedList<>();

Here, we have created objects of ArrayList and LinkedList classes list1and list2.Now these objects can use the functions of the List interface.

List methods

The List interface includes all methods of the Collection interface. This is because Collection is the superinterface of List.

The Collection interface also provides some commonly used List interface methods:

  • add() - Add element to the list

  • addAll() - Add all elements of one list to another

  • get() - Helps to access elements randomly from the list

  • iterator() - Return an iterator object that can be used to sequentially access the elements of the list

  • set() - Change the element in the list

  • remove() - Remove an element from the list

  • removeAll() - Remove all elements from the list

  • clear() - Remove all elements from the list (more efficient than removeAll())

  • size() - Return the length of the list

  • toArray() - Convert the list to an array

  • contains() -  Returns true if the list contains the specified element

List interface implementation

1.Implement ArrayList class

import java.util.List;
import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        //Create a list using the ArrayList class
        List<Integer> numbers = new ArrayList<>();
        //Add element to the list
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println("List: " + numbers);
        //Access element from the list
        int number = numbers.get(2);
        System.out.println("Access element: " + number);
        //Remove element from the list
        int removedNumber = numbers.remove(1);
        System.out.println("Delete element: ", + removedNumber);
    }
}

Output result

List: [1, 2, 3]
Access element: 3
Delete element: 2

For more information about ArrayList, please visitJava ArrayList.

2.Implement LinkedList class

import java.util.List;
import java.util.LinkedList;
class Main {
    public static void main(String[] args) {
        //Create a list using the LinkedList class
        List<Integer> numbers = new LinkedList<>();
        //Add element to the list
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println("List: " + numbers);
        //Access element from the list
        int number = numbers.get(2);
        System.out.println("Access element: " + number);
        //Use the indexOf() method
        int index = numbers.indexOf(2);
        System.out.println("Position3The element is " + index);
        //Remove element from the list
        int removedNumber = numbers.remove(1);
        System.out.println("Delete element: ", + removedNumber);
    }
}

Output result

List: [1, 2, 3]
Access element: 3
at position3The element is 1
Delete element: 2

For more information about LinkedList, please visitJava LinkedList.

Java List and Set

Both the List interface and the Set interface inherit from the Collection interface. However, there are some differences between them.

  • A List can contain duplicate elements. However, a Set cannot have duplicate elements.

  • The elements in a List are stored in some order. However, the elements in a Set are stored in groups, just like sets in mathematics.

Now that we know what a List is, we will go into detail about the implementation of the ArrayList and LinkedList classes in the next tutorial.