Introduction

We will study the Java List interface and its methods in this tutorial. The List interface in Java is a sequentially accessed ordered collection that lets us store elements. The Collection interface is expanded by it..

Classes that Implement List

  • List is an interface, so we are unable to make objects from it. These classes can be used to access features of the List interface:
  • ArrayList
  • LinkedList
  • Vector
  • Stack
Classes implementing the List interface in Java

The List interface is implemented by these classes, which are described in the Collections framework.

How to use List?

Use of List in Java requires the import of the java.util.List package.

// ArrayList implementation of List
List<String> list1 = new ArrayList<>();

// LinkedList implementation of List
List<String> list2 = new LinkedList<>();

Here, we've made objects of the classes ArrayList and LinkedList called list1 and list2, respectively. These objects can make use of the List interface's features.

Methods of List

All of the Collection interface's methods are available on the List interface. It's because Collection is a superset of List's interface. The following are some frequently used Collection interface methods that are also offered in the List interface:

  • add() - expands a list by one element
  • addAll() - adds all of a list's components to another.
  • get() - aids in accessing random elements from lists
  • iterator() - produces an iterator object that may be used to access items of lists consecutively.
  • set() - alters list items
  • remove() - removes a list item from it
  • removeAll() - removes all of the list's elements
  • clear() - removes all of the list's elements (more quickly than removeAll()).
  • size() - lists' length is returned
  • toArray() - list to array conversion
  • contains() - returns true if the supplied element is present in the list.

Implementation of the List Interface

Implementing the ArrayList Class

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

class Main {

    public static void main(String[] args) {
        // Creating list using the ArrayList class
        List<Integer> numbers = new ArrayList<>();

        // Add elements 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("Accessed Element: " + number);

        // Remove element from the list
        int removedNumber = numbers.remove(1);
        System.out.println("Removed Element: " + removedNumber);
    }
}



Output
List: [1, 2, 3]
Accessed Element: 3
Removed Element: 2

Implementing the LinkedList Class

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

class Main {

    public static void main(String[] args) {
        // Creating list using the LinkedList class
        List<Integer> numbers = new LinkedList<>();

        // Add elements 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("Accessed Element: " + number);

        // Using the indexOf() method
        int index = numbers.indexOf(1);
        System.out.println("Position ->2: " + index);

        // Remove element from the list
        int removedNumber = numbers.remove(1);
        System.out.println("Removed Element: " + removedNumber);
    }
}


Output
List: [1, 2, 3]
Accessed Element: 3
Position ->2: 1
Removed Element: 2

Java List vs. Set

The Collection interface is inherited by both the List interface and the Set interface. There are some differences between them, though. Duplicate items are permitted in lists. Sets, however, are unable to include duplicate items. List elements are kept in a certain sequence. However, much like in mathematics, set elements are kept in groups.