๐งบ 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
๐
ArrayList
is part of thejava.util
package and implements theList
interface.
๐ 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
List
interface when declaring:List<String> list = new ArrayList<>();
- โ ๏ธ
ArrayList
is 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 :