Java ArrayList Methods

About This Index
This page is the Java ArrayList Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, so the output shown is the output you get.
Learn what an ArrayList is on the ArrayList hub. Use this page when you need a specific method such as add(), get(), or remove(). The examples compile on Java 8 and later, except on the six Java 21 pages listed below.
The first element of an ArrayList is at index 0 and the last at size() - 1. Printing a list shows its elements in index order, like [a, b, c]. The methods addFirst(), addLast(), getFirst(), getLast(), removeFirst(), and removeLast() need Java 21 or newer.
How to Call an ArrayList Method
Every method here is an instance method, so you call it on your list variable:
import java.util.ArrayList;
import java.util.List;
List<String> list = new ArrayList<>();
list.add("b"); // [b]
list.add(0, "a"); // [a, b]
list.add("c"); // [a, b, c]
String second = list.get(1); // "b"
list.set(1, "B"); // [a, B, c]
int count = list.size(); // 3 Java ArrayList Methods Table
All method guides in this Java tutorial series (31 tutorials), grouped by purpose.
Add elements
4 methodsAppend at the end, insert at an index, or add a whole collection. The list grows by itself; addFirst and addLast need Java 21.
Read and replace
5 methodsRead an element by index or at either end, replace one element, or replace every element with a function.
| Method | Description | Tutorial |
|---|---|---|
get() | Read the element at an index. | Try it |
getFirst() | Read the first element (Java 21+); throws if empty. | Try it |
getLast() | Read the last element (Java 21+); throws if empty. | Try it |
set() | Replace the element at an index. | Try it |
replaceAll() | Replace every element using a function (Java 8+). | Try it |
Remove elements
7 methodsRemove by index, by value, at either end, by another collection, by a condition, or everything at once.
| Method | Description | Tutorial |
|---|---|---|
remove() | Remove one element by index or by value. | Try it |
removeFirst() | Remove and return the first element (Java 21+). | Try it |
removeLast() | Remove and return the last element (Java 21+). | Try it |
removeAll() | Remove every element found in another collection. | Try it |
retainAll() | Keep only elements found in another collection. | Try it |
removeIf() | Remove every element that matches a condition (Java 8+). | Try it |
clear() | Remove all elements at once. | Try it |
Search
3 methodsCheck whether a value is present or find its first or last index. All three use equals() and return false or -1 when nothing matches.
| Method | Description | Tutorial |
|---|---|---|
contains() | Check whether the list holds a value. | Try it |
indexOf() | Find the first index of a value, or -1. | Try it |
lastIndexOf() | Find the last index of a value, or -1. | Try it |
Iterate
4 methodsWalk the elements in index order, go backwards, edit while looping, or split the list for streams.
| Method | Description | Tutorial |
|---|---|---|
iterator() | Walk the list and remove safely while looping. | Try it |
listIterator() | Iterate both ways and edit while looping. | Try it |
forEach() | Run an action for every element (Java 8+). | Try it |
spliterator() | Split the list for streams and parallel work (Java 8+). | Try it |
Sort, view, and copy
4 methodsSort in place, work on a range through a view, or copy the elements into a new list or an array.
Size and capacity
4 methodsCount the elements, check for an empty list, or manage the internal array that holds them.
| Method | Description | Tutorial |
|---|---|---|
size() | Count the elements in the list. | Try it |
isEmpty() | Check whether the list has no elements. | Try it |
ensureCapacity() | Reserve room before adding many elements. | Try it |
trimToSize() | Shrink the capacity to the current size. | Try it |
Frequently Asked Questions
Start with add()
Put your first elements into an ArrayList and see where they go.
12 people found this page helpful
