Java Reference ยท Collections
The List interface
Reference for java.util.List: ordered collections with index access, duplicates, key methods, immutable factories and the difference between implementations.
java.util.List<E> is an ordered collection that allows duplicates and gives access to elements by their zero-based position. It is the most used collection type in Java. A list remembers the order in which elements were added (or placed with an index), and you can insert, replace and remove elements at any position.
List is only an interface. The usual implementations are ArrayList (fast random access), LinkedList (cheap insertion at the ends), and the immutable lists returned by List.of(). Declare variables as List<E> so that you can switch implementations later.
List<String> names = new ArrayList<>(); List<Integer> fixed = List.of(1, 2, 3);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public interface List<E> extends SequencedCollection<E>
| Method | Description |
|---|---|
E get(int index) |
Element at index. |
E set(int index, E element) |
Replaces the element and returns the old one. |
void add(int index, E element) |
Inserts, shifting later elements right. |
E remove(int index) |
Removes by position and returns the element. |
int indexOf(Object o) / lastIndexOf(Object o) |
First/last position, or -1. |
List<E> subList(int from, int to) |
A view of the range [from, to). |
void sort(Comparator<? super E> c) |
Sorts in place; null means natural order. |
void replaceAll(UnaryOperator<E> op) |
Replaces every element with op's result. |
ListIterator<E> listIterator() |
Bidirectional iterator. |
static <E> List<E> of(E... elements) |
Immutable list (Java 9). |
static <E> List<E> copyOf(Collection<? extends E> c) |
Immutable copy (Java 10). |
getFirst(), getLast(), reversed(), addFirst(), removeLast() and others |
Sequenced operations (Java 21). |
Throws: IndexOutOfBoundsException for an invalid index; UnsupportedOperationException on immutable or fixed-size lists.
Usage notes
remove(int)versusremove(Object): on aList<Integer>,list.remove(1)removes the element at index 1; uselist.remove(Integer.valueOf(1))to remove the value 1.equals()compares elements in order and works across implementations. The hash code is computed from the elements as well, so do not mutate a list that is used as aHashMapkey.List.of()rejectsnulland is immutable;Arrays.asList()is fixed-size but allowsset();new ArrayList<>(...)is fully mutable.- Performance: indexed access is
O(1)inArrayListandO(n)inLinkedList. See Collection complexity. - Insertion at the start or middle of an
ArrayListshifts the tail, which costsO(n). - Iteration: the for-each loop is fine; to remove while looping use
Iterator.remove()orremoveIf(). getFirst(),getLast(),reversed()exist since Java 21; on earlier versions useget(0)andget(size() - 1).