๐Ÿ“š Java Reference
Estimated reading: 4 minutes 33 views

๐Ÿงบ 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 the java.util package and implements the List 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
Addadd(), add(index, element)
Get/Setget(), set()
Deleteremove(), clear()
Checkcontains(), isEmpty()
Size/Infosize(), indexOf(), lastIndexOf()
ConverttoArray()
Sort/IterateCollections.sort(), forEach()
BulkretainAll(), 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. Use Collections.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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Java ArrayList Methods

Or Copy Link

CONTENTS
Scroll to Top