java.util · java.base · since Java 1.2
List
public interface List<E> extends SequencedCollection<E>An ordered, indexed sequence allowing duplicates. The interface to declare against; ArrayList is the default implementation.
- List.of(...) has fixed-arity overloads for 0–10 elements plus a varargs form, avoiding an extra array allocation for common literal sizes
- addFirst/addLast/getFirst/getLast (SequencedCollection, Java 21) are convenience defaults — on ArrayList only addFirst is an O(n) shift; addLast is amortized O(1) and getFirst/getLast are O(1)
Key methods
E get(int) / E set(int, E) | Positional access. |
void add(int, E) / E remove(int) | Positional insertion/removal (shifts). |
boolean contains(Object) | Membership via equals() — O(n) unless the implementation says otherwise. |
int indexOf(Object) / lastIndexOf(Object) | First/last matching position via equals(), or -1. |
boolean addAll(int, Collection<? extends E>) | Bulk insert at a position. |
void replaceAll(UnaryOperator<E>) | In-place transform of every element. |
ListIterator<E> listIterator() / listIterator(int) | Bidirectional cursor supporting set()/add() mid-iteration — richer than Iterator. |
static <E> List<E> of(E...) | Immutable, null-hostile literal lists. |
static <E> List<E> copyOf(Collection<E>) | Immutable defensive copy. |
List<E> subList(int from, int to) | Write-through range VIEW. |
void sort(Comparator<? super E>) | In-place stable sort (null = natural order). |
List<E> reversed() | Reversed view (SequencedCollection, Java 21). |
E getFirst() / E getLast() | Head/tail access, throws if empty (SequencedCollection, Java 21). |
void addFirst(E) / addLast(E) | Head/tail insertion (SequencedCollection, Java 21). |
boolean equals(Object) / int hashCode() | Content equality by element order — independent of the concrete implementation. |