Java ArrayList Methods – Full Guide with Syntax, Examples & Best Practices (2025)
Introduction – Why Use Java ArrayList Methods?
In Java, ArrayList is one of the most popular dynamic data structures. Unlike arrays, it can grow and shrink as needed — and its rich set of built-in methods makes it ideal for modern, flexible applications.
By mastering ArrayList methods, you can:
- Easily add, remove, search, and sort elements
- Write cleaner, more efficient code
- Avoid boilerplate logic with powerful built-in utilities
ArrayListis part of thejava.utilpackage and implements theListinterface.
What Is an ArrayList in Java?
An ArrayList is a resizable array backed by an internal array. It automatically handles memory allocation, making it ideal for collections where size may vary.
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
Common Java ArrayList Methods
| Method | Purpose |
|---|---|
add() | Adds element to list |
add(index, element) | Inserts at specified position |
get(index) | Gets element at index |
set(index, element) | Replaces element at index |
remove(index/object) | Removes by index or object |
clear() | Removes all elements |
contains() | Checks if element exists |
isEmpty() | Checks if list is empty |
size() | Returns number of elements |
indexOf() / lastIndexOf() | Gets position of element |
toArray() | Converts to array |
sort() | Sorts elements (with Collections.sort()) |
forEach() | Iterates using lambda |
retainAll() / removeAll() | Bulk operations |
Java ArrayList Method Examples
1. add() – Add Elements to ArrayList
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
System.out.println(fruits);
Output:
[Apple, Banana]
Adds elements to the list.
2. add(index, element) – Insert at Position
fruits.add(1, "Mango");
System.out.println(fruits);
Output:
[Apple, Mango, Banana]
Inserts element at index 1.
3. get(index) – Access Element
System.out.println(fruits.get(0)); // Apple
Fetches the element at index 0.
4. set(index, element) – Replace Value
fruits.set(1, "Orange");
System.out.println(fruits);
Output:
[Apple, Orange, Banana]
Replaces “Mango” with “Orange”.
5. remove() – Delete by Index or Value
fruits.remove("Banana");
System.out.println(fruits);
Output:
[Apple, Orange]
Removes the first occurrence of “Banana”.
6. clear() – Empty the List
fruits.clear();
System.out.println(fruits.isEmpty()); // true
Clears all elements from the list.
7. contains() – Check for Element
fruits.add("Kiwi");
System.out.println(fruits.contains("Kiwi")); // true
Checks if the list has “Kiwi”.
8. size() – List Length
System.out.println(fruits.size()); // 1
Returns the number of elements.
9. indexOf() and lastIndexOf()
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Blue");
colors.add("Red");
System.out.println(colors.indexOf("Red")); // 0
System.out.println(colors.lastIndexOf("Red")); // 2
Finds first and last index of an element.
10. toArray() – Convert to Array
String[] array = fruits.toArray(new String[0]);
System.out.println(Arrays.toString(array));
Converts list to array format.
11. forEach() – Iterate with Lambda
fruits.forEach(fruit -> System.out.println("Fruit: " + fruit));
Uses Java 8+ lambda for cleaner iteration.
12. retainAll() and removeAll() – Bulk Ops
ArrayList<String> a = new ArrayList<>(List.of("A", "B", "C"));
ArrayList<String> b = new ArrayList<>(List.of("B", "C", "D"));
a.retainAll(b);
System.out.println(a); // [B, C]
Keeps only common elements between two lists.
Summary Table of Methods
| Operation | Methods |
|---|---|
| Add | add(), add(index, element) |
| Get/Set | get(), set() |
| Delete | remove(), clear() |
| Check | contains(), isEmpty() |
| Size/Info | size(), indexOf(), lastIndexOf() |
| Convert | toArray() |
| Sort/Iterate | Collections.sort(), forEach() |
| Bulk | retainAll(), removeAll() |
Tips for Using ArrayList Methods
- Use generics like
ArrayList<String>to avoid type-casting. - Prefer
Listinterface when declaring:List<String> list = new ArrayList<>(); -
ArrayListis not thread-safe. UseCollections.synchronizedList()for multithreading. - Avoid frequent
add(0, item)calls — it’s slow due to shifting.
Summary – Why Mastering ArrayList Methods Matters
ArrayList methods are versatile, powerful, and essential for working with collections in Java. They help you:
- Add, access, and remove data efficiently
- Perform complex operations like search, filter, and sort
- Build dynamic, user-friendly applications
Mastering these methods gives you a strong foundation in Java collections and object-oriented programming.
FAQs on Java ArrayList Methods
Is ArrayList ordered?
Yes. Elements maintain insertion order.
Can ArrayList contain duplicate elements?
Yes. Duplicates are allowed.
How is ArrayList different from array?
- Array: Fixed size, primitive types
- ArrayList: Resizable, holds objects
How to sort an ArrayList?
Collections.sort(list);
How to iterate over an ArrayList?
Use for, for-each, Iterator, or forEach() with lambda.
Share Now :
