ArrayList

Ordered, resizable array implementation. Fast random access, slower insertions or deletions of elements near the lower indexes.

Common methods:

  • add(E e) - appends element to end
  • get(int index) - retrieves element at index
  • remove(int index) - removes element at index
  • size() - returns number of elements
  • contains(Object o) - checks if element exists
  • set(int index, E e) - set element at index

LinkedList

Doubly-linked list implementation. Fast insertions/deletions at ends, slower random access.

Common methods:

  • add(E e) - appends to end
  • addFirst(E e) / addLast(E e) - adds to front/back
  • removeFirst() / removeLast() - removes and returns first/last
  • get(int index) - retrieves element (slow)
  • size() - returns number of elements

ArrayDeque

Resizable array-based double-ended queue. Fast additions/removals at both ends, no capacity restrictions.

Common methods:

  • addFirst(E e) / addLast(E e) - adds to front/back
  • removeFirst() / removeLast() - removes and returns first/last
  • peekFirst() / peekLast() - views first/last without removing
  • push(E e) / pop() - stack operations (front of deque)

PriorityQueue

Heap-based priority queue. Elements ordered by natural ordering or custom comparator, not by insertion order.

Common methods:

  • add(E e) / offer(E e) - inserts element
  • poll() - removes and returns smallest element
  • peek() - views smallest element without removing
  • remove(Object o) - removes specific element
  • size() - returns number of elements

Stack

LIFO (Last-In-First-Out) data structure. Legacy class - ArrayDeque is preferred for stack operations.

Common methods:

  • push(E e) - adds element to top
  • pop() - removes and returns top element
  • peek() - views top element without removing
  • empty() - checks if stack is empty
  • search(Object o) - returns 1-based position from top (returns -1 if not found)

Modern alternative: Use ArrayDeque with push(), pop(), and peek() methods.

Queue (Interface)

FIFO (First-In-First-Out) data structure interface. Common implementations: LinkedList, ArrayDeque, PriorityQueue.

Common methods:

  • add(E e) / offer(E e) - inserts element (offer returns false if fails, add throws exception)
  • remove() / poll() - removes and returns head (poll returns null if empty, remove throws exception)
  • element() / peek() - views head without removing (peek returns null if empty, element throws exception)
  • size() - returns number of elements
  • isEmpty() - checks if queue is empty

Collections.sort()

Sorts a List in-place. Uses modified mergesort (Timsort), stable, O(n log n).

Usage:

  • Collections.sort(List<T> list) - sorts using natural ordering
  • Collections.sort(List<T> list, Comparator<T> c) - sorts using custom comparator

Arrays.sort()

Sorts an array in-place. Uses dual-pivot quicksort for primitives, Timsort for objects. O(n log n) average.

Usage:

  • Arrays.sort(array) - sorts entire array
  • Arrays.sort(array, fromIndex, toIndex) - sorts range
  • Arrays.sort(array, Comparator<T> c) - sorts objects with custom comparator