Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

Java List.add() and remove() methods

Reference for Java List.add(), addAll(), remove() and clear(): insert at an index, remove by index or value, return values and the Integer overload trap.

add() and remove() are the basic methods for changing the contents of a List. add(e) appends an element, add(index, e) inserts it at a position and shifts the rest to the right. remove(index) removes by position, while remove(Object) removes the first element that equals the argument.

They have two traps. First, remove(int) and remove(Object) are different overloads, which matters for List<Integer>. Second, both are optional operations: lists from List.of() or Arrays.asList() reject them with UnsupportedOperationException.

Java
list.add("x");
list.add(0, "first");
list.remove(0);
list.remove("x");

Try it

Java Adding and removing

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.

Java The remove(int) versus remove(Object) trap

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

Java
boolean add(E e)
void add(int index, E element)
boolean addAll(Collection<? extends E> c)
boolean addAll(int index, Collection<? extends E> c)
E remove(int index)
boolean remove(Object o)
void clear()
Name Description
e, element The element to insert; null is allowed by ArrayList and LinkedList.
index Zero-based position. For add, 0..size() is valid; for remove, 0..size()-1.
c Collection whose elements are inserted in its iteration order.
o Object to remove, compared with equals().

Returns: add(e) returns true; remove(int) returns the removed element; remove(Object) returns true if something was removed; addAll returns true if the list changed.

Throws: IndexOutOfBoundsException for an invalid index; UnsupportedOperationException on immutable or fixed-size lists; NullPointerException if c is null or the list forbids null.

Usage notes

  • Cost in ArrayList: add(e) is amortized O(1); inserting or removing elsewhere shifts elements, O(n). LinkedList is O(1) at the ends but needs O(n) to find an index.
  • remove(Object) removes only the first match. Use removeAll() or removeIf() for all.
  • Removing in a loop: an index loop must adjust the index or run backwards; a for-each loop must not modify the list. See removeIf() and replaceAll().
  • addAll(this) adds a snapshot of the current content; it does not loop forever.
  • Java 21 additions: addFirst(), addLast(), removeFirst() and removeLast() are available on every List.
  • add() on a subList changes the parent list as well.