Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java List Interface: Overview, Implementations, and Key Methods

Java List Interface

Explore the Java List interface—its purpose, common implementations, essential methods, and how it differs from Set.

In Java, the List interface represents an ordered, index‑based collection that supports duplicates and random access. It extends Collection and is part of the Collections Framework.

Key Implementations of List

Because List is an interface, you instantiate concrete classes to use its functionality:

Java List Interface: Overview, Implementations, and Key Methods

These classes are defined within the java.util package.

Using List in Code

First import java.util.List (and the concrete class you wish to use).

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

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

The variables arrayList and linkedList now expose all List methods.

Essential List Methods

All methods listed below are inherited from Collection and are available on every List implementation.

ArrayList in Practice

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

public class ArrayListDemo {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();

        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println("List: " + numbers);

        int third = numbers.get(2);
        System.out.println("Accessed Element: " + third);

        int removed = numbers.remove(1);
        System.out.println("Removed Element: " + removed);
    }
}

Output

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

For deeper insight into ArrayList, consult the official Oracle documentation.

LinkedList in Practice

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

public class LinkedListDemo {
    public static void main(String[] args) {
        List<Integer> numbers = new LinkedList<>();

        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println("List: " + numbers);

        int third = numbers.get(2);
        System.out.println("Accessed Element: " + third);

        int index = numbers.indexOf(2);
        System.out.println("Position of 2 is " + index);

        int removed = numbers.remove(1);
        System.out.println("Removed Element: " + removed);
    }
}

Output

List: [1, 2, 3]
Accessed Element: 3
Position of 2 is 1
Removed Element: 2

Further details on LinkedList are available in the Java Tutorials.

List vs. Set

Both interfaces extend Collection but differ in key aspects:

Choosing between them depends on whether order and duplicates matter for your use case.

Continue exploring concrete implementations in upcoming tutorials.

Java

  1. Java TreeMap: Master Sorted Maps with Practical Examples
  2. Mastering Java ConcurrentHashMap: Operations, Examples, and Best Practices
  3. Java HashSet Class: Complete Guide to Operations, Methods, and Set Theory
  4. Mastering Java TreeSet: Operations, Methods, and Practical Examples
  5. Mastering Java Collection Algorithms: Sorting, Shuffling, and More
  6. Master Java Autoboxing and Unboxing: Practical Examples and Best Practices
  7. Generate Random Numbers in Java: Practical Guide with Random and Math.random
  8. Java Numbers Class – Wrapper Classes & Inheritance Explained
  9. Java 10: 70+ New APIs & Features Explained
  10. Java 9 Collection Factory Methods: Simplify Immutable List, Set & Map Creation