Java ArrayList and LinkedList – Complete Guide with Examples


Learn Java ArrayList and LinkedList, their differences, methods, and how to use these List implementations to store, access, and manipulate dynamic collections efficiently.

ArrayList and LinkedList in Java – Complete Detailed Tutorial

In Java, List interface is implemented by ArrayList and LinkedList. Both store ordered collections and allow duplicate elements, but differ in internal implementation and performance.

1. ArrayList

  1. Resizable array implementation of List
  2. Dynamic size, unlike arrays
  3. Allows duplicates, maintains insertion order
  4. Provides fast random access (get(index))
  5. Slower insertions/deletions in the middle

Common Methods:

MethodDescription
add(E e)Adds element at end
add(int index, E e)Adds element at specified index
get(int index)Returns element at index
remove(int index)Removes element at index
size()Returns number of elements
contains(Object o)Checks if element exists

Example – Using ArrayList


import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // duplicate allowed

System.out.println("ArrayList: " + fruits);
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
System.out.println("Element at index 1: " + fruits.get(1));
}
}

Output:


ArrayList: [Apple, Banana, Orange, Apple]
After removal: [Apple, Orange, Apple]
Element at index 1: Orange

2. LinkedList

  1. Doubly linked list implementation of List and Deque
  2. Maintains insertion order, allows duplicates
  3. Faster insertions/deletions in the middle than ArrayList
  4. Slower random access (get(index))

Common Methods:

MethodDescription
add(E e)Adds element at end
addFirst(E e)Adds element at beginning
addLast(E e)Adds element at end
get(int index)Returns element at index
removeFirst()Removes first element
removeLast()Removes last element
size()Returns number of elements

Example – Using LinkedList


import java.util.LinkedList;
import java.util.List;

public class Main {
public static void main(String[] args) {
List<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // duplicate allowed

System.out.println("LinkedList: " + fruits);
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
System.out.println("First element: " + ((LinkedList<String>) fruits).getFirst());
System.out.println("Last element: " + ((LinkedList<String>) fruits).getLast());
}
}

Output:


LinkedList: [Apple, Banana, Orange, Apple]
After removal: [Apple, Orange, Apple]
First element: Apple
Last element: Apple

3. ArrayList vs LinkedList

FeatureArrayListLinkedList
ImplementationResizable arrayDoubly linked list
Access timeFast random access (get(index))Slow random access
Insert/DeleteSlow (middle elements)Fast (middle elements)
MemoryLess overheadMore memory (pointers)
Use caseFrequent accessFrequent insertion/deletion

4. Key Points

  1. ArrayList: best for frequent access, less insertion/deletion
  2. LinkedList: best for frequent insertion/deletion, sequential access
  3. Both implement List interface, maintain insertion order, and allow duplicates
  4. Can use Iterator to traverse both

5. Summary

  1. ArrayList: dynamic array, fast access, slower insert/delete
  2. LinkedList: doubly linked list, fast insert/delete, slower access
  3. Both are part of Java Collections Framework and widely used in applications