The List interface in Java is part of the java.util package and represents an ordered collection of elements. It allows duplicate values and provides positional access to elements.
The List interface is implemented by multiple classes. You cannot create an object of List directly, but you can reference it using implementing classes.
// Declaring a List reference with ArrayList implementation
List names = new ArrayList<>();
// Demonstrating basic List operations
import java.util.*;
public class ListDemo {
public static void main(String[] args) {
List numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(10);
System.out.println(numbers);
}
}
[10, 20, 10]
The List preserves insertion order and allows duplicate elements.
Visualize how ArrayList.add() and remove() work. Notice the indices updates!
List) over class referenceset() method