Every ArrayList Method You Need to Know (With Examples)

java dev.to

Before anything, this is the import you need:

import java.util.ArrayList;
Enter fullscreen mode Exit fullscreen mode

And this is how you create one:

ArrayList<String> list = new ArrayList<>();
Enter fullscreen mode Exit fullscreen mode

Simple. Now let's get into the methods.


The Methods

add(element)

Adds an element to the end of the list.

list.add("Apple");
Enter fullscreen mode Exit fullscreen mode

add(index, element)

Adds an element at a specific position. Shifts everything else to the right.

list.add(1, "Banana");
Enter fullscreen mode Exit fullscreen mode

get(index)

Returns the element at that index.

String fruit = list.get(0); // Apple
Enter fullscreen mode Exit fullscreen mode

set(index, element)

Replaces the element at that index with a new one.

list.set(0, "Mango"); // Apple becomes Mango
Enter fullscreen mode Exit fullscreen mode

remove(index)

Removes the element at that index.

list.remove(1); // removes Banana
Enter fullscreen mode Exit fullscreen mode

remove(object)

Removes the first occurrence of that specific value.

list.remove("Mango");
Enter fullscreen mode Exit fullscreen mode

Heads up — if your list is ArrayList<Integer>, Java gets confused between remove(index) and remove(object). Wrap the value: list.remove(Integer.valueOf(5)).


size()

Returns how many elements are in the list.

int count = list.size();
Enter fullscreen mode Exit fullscreen mode

isEmpty()

Returns true if the list has no elements.

boolean empty = list.isEmpty();
Enter fullscreen mode Exit fullscreen mode

contains(object)

Returns true if the list has that value.

boolean has = list.contains("Apple");
Enter fullscreen mode Exit fullscreen mode

indexOf(object)

Returns the index of the first occurrence. Returns -1 if not found.

int pos = list.indexOf("Apple");
Enter fullscreen mode Exit fullscreen mode

lastIndexOf(object)

Returns the index of the last occurrence. Useful when duplicates exist.

int lastPos = list.lastIndexOf("Apple");
Enter fullscreen mode Exit fullscreen mode

clear()

Removes everything. List becomes empty.

list.clear();
Enter fullscreen mode Exit fullscreen mode

addAll(collection)

Adds all elements from another collection into this list.

ArrayList<String> more = new ArrayList<>();
more.add("Grapes");
more.add("Papaya");
list.addAll(more);
Enter fullscreen mode Exit fullscreen mode

addAll(index, collection)

Same as above but inserts at a specific position.

list.addAll(1, more);
Enter fullscreen mode Exit fullscreen mode

removeAll(collection)

Removes all elements that exist in the given collection.

list.removeAll(more);
Enter fullscreen mode Exit fullscreen mode

retainAll(collection)

Keeps only the elements that exist in the given collection. Removes everything else.

list.retainAll(more);
Enter fullscreen mode Exit fullscreen mode

containsAll(collection)

Returns true if this list contains every element in the given collection.

boolean hasAll = list.containsAll(more);
Enter fullscreen mode Exit fullscreen mode

toArray()

Converts the ArrayList into a plain array.

Object[] arr = list.toArray();
Enter fullscreen mode Exit fullscreen mode

subList(fromIndex, toIndex)

Returns a portion of the list. fromIndex is included, toIndex is not.

List<String> part = list.subList(0, 2); // index 0 and 1 only
Enter fullscreen mode Exit fullscreen mode

Collections.sort(list)

Not an ArrayList method exactly, but used all the time. Sorts the list in ascending order.

import java.util.Collections;
Collections.sort(list);
Enter fullscreen mode Exit fullscreen mode

Collections.reverse(list)

Reverses the order of elements.

Collections.reverse(list);
Enter fullscreen mode Exit fullscreen mode

Quick Reference Table

Method What it does
add(element) Adds to end
add(index, element) Inserts at position
get(index) Returns element at index
set(index, element) Replaces element at index
remove(index) Removes by position
remove(object) Removes by value
size() Returns total count
isEmpty() Checks if list is empty
contains(object) Checks if value exists
indexOf(object) First position of value
lastIndexOf(object) Last position of value
clear() Removes everything
addAll(collection) Merges another list
addAll(index, collection) Merges at a position
removeAll(collection) Removes matching elements
retainAll(collection) Keeps only matching elements
containsAll(collection) Checks if all exist
toArray() Converts to plain array
subList(from, to) Returns a slice of the list
Collections.sort() Sorts ascending
Collections.reverse() Reverses the list

Source: dev.to

arrow_back Back to Tutorials