Syntax
ArrayList<Type> list = new ArrayList<>();Examples
Creating and Adding Elements
Building an ArrayList and adding items to it.
import java.util.ArrayList;
ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");
System.out.println(fruits); // [apple, banana, cherry]
System.out.println(fruits.size()); // 3Accessing and Modifying Elements
Reading, updating, and removing elements by index or value.
import java.util.ArrayList;
ArrayList<String> names = new ArrayList<>();
names.add("Fola");
names.add("Zain");
names.add("Jamal");
System.out.println(names.get(1)); // Zain
names.set(1, "Priya"); // replace element at index 1
System.out.println(names); // [Fola, Priya, Jamal]
names.remove("Jamal"); // remove by value
names.remove(0); // remove by index
System.out.println(names); // [Priya]Iterating Over an ArrayList
Looping through elements with a for-each loop.
import java.util.ArrayList;
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
for (int num : numbers) {
System.out.println(num);
}
// Checking membership and finding an index
System.out.println(numbers.contains(20)); // true
System.out.println(numbers.indexOf(30)); // 2Sorting an ArrayList
Using Collections.sort() or the built-in sort() method with a comparator.
import java.util.ArrayList;
import java.util.Collections;
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(1);
numbers.add(3);
Collections.sort(numbers);
System.out.println(numbers); // [1, 3, 5]
numbers.sort(Collections.reverseOrder());
System.out.println(numbers); // [5, 3, 1]Best practices
- Use ArrayList when you need fast index-based access and mostly add elements at the end - avoid frequent inserts/removals in the middle, which are slow
- Declare the variable using the List interface (List<String> list = new ArrayList<>();) so the implementation can be swapped later without changing calling code
- Use an enhanced for-each loop for reading, but a regular Iterator (or indexed loop counting down) when removing elements while iterating, to avoid a ConcurrentModificationException
- Specify an initial capacity (new ArrayList<>(100)) when you know roughly how many elements to expect, to reduce internal resizing
At a glance
- Purpose
- General-purpose application development
- File extension
- .java
- Runs in
- Java Virtual Machine
- Usually used with
- JDK and Java libraries
Specifications & further reading
Related Java documentation
LinkedList
LinkedList is a doubly-linked list implementation of both the List and Deque interfaces. Unlike ArrayList, it stores elements as individual nodes linked to their neighbors, which makes inserting and removing elements at the beginning or middle much faster, at the cost of slower random access by index. Because it implements Deque, LinkedList can also be used directly as a stack or queue.HashMap
HashMap stores data as key-value pairs, offering constant-time (average case) lookup, insertion, and deletion by key, backed by a hash table. Keys must be unique - adding a value with an existing key overwrites the previous value. HashMap does not guarantee any particular ordering of its entries, unlike LinkedHashMap (which preserves insertion order) or TreeMap (which keeps keys sorted).HashSet
HashSet is a collection that stores unique elements with no guaranteed ordering, backed by a HashMap internally. Adding a duplicate element has no effect, since HashSet automatically enforces uniqueness. It provides constant-time (average case) performance for adding, removing, and checking membership, making it ideal for deduplication and fast lookups.TreeMap & TreeSet
TreeMap and TreeSet are sorted collections backed by a red-black tree, automatically keeping their keys (or elements) in ascending order at all times. This ordering comes at a cost - operations run in logarithmic time rather than the constant time of HashMap/HashSet - but it's invaluable when you need sorted iteration, range queries, or to quickly find the smallest or largest element.
LinkedList is a doubly-linked list implementation of both the List and Deque interfaces. Unlike ArrayList, it stores elements as individual nodes linked to their neighbors, which makes inserting and removing elements at the beginning or middle much faster, at the cost of slower random access by index. Because it implements Deque, LinkedList can also be used directly as a stack or queue.HashMap
HashMap stores data as key-value pairs, offering constant-time (average case) lookup, insertion, and deletion by key, backed by a hash table. Keys must be unique - adding a value with an existing key overwrites the previous value. HashMap does not guarantee any particular ordering of its entries, unlike LinkedHashMap (which preserves insertion order) or TreeMap (which keeps keys sorted).HashSet
HashSet is a collection that stores unique elements with no guaranteed ordering, backed by a HashMap internally. Adding a duplicate element has no effect, since HashSet automatically enforces uniqueness. It provides constant-time (average case) performance for adding, removing, and checking membership, making it ideal for deduplication and fast lookups.TreeMap & TreeSet
TreeMap and TreeSet are sorted collections backed by a red-black tree, automatically keeping their keys (or elements) in ascending order at all times. This ordering comes at a cost - operations run in logarithmic time rather than the constant time of HashMap/HashSet - but it's invaluable when you need sorted iteration, range queries, or to quickly find the smallest or largest element.