beginnerFree

Java Collections: The Family

Know the landscape before picking a collection

Java Collections: The Family

Java collections organize into interfaces - List, Set, Queue, and Map.

The narrated version of this skribble — tap play for the full-size video.

0:00 / 1:19

The Big Picture

The Collections Framework organizes Java's data structures.

  • Under Collection: List, Set, Queue/Deque
  • Map is a related interface, not a Collection
  • Common interface lets you swap implementations easily
  • Part of java.util since Java 1.2

Every Java program beyond basics uses collections.

How It Works

Collection types are Iterable (for-each); Map is not - iterate its keySet/values/entrySet views.

  • List: ordered, allows duplicates, index access
  • Set: unique elements, no guaranteed order
  • Map: key-value pairs, unique keys
  • Queue/Deque: order depends on impl - FIFO queues, deque both ends, PriorityQueue by priority
  • Backed by arrays, linked lists, hash tables, or trees

In the Java Landscape

Java 9+ factory methods: List.of(), Set.of(), Map.of()

  • These create unmodifiable collections
  • Streams (Java 8+) transform collections functionally
  • Collections utility class: sort, shuffle, sync wrappers
  • Guava and Apache Commons extend the framework
  • Concurrent collections in java.util.concurrent

Pitfalls and Best Practices

  • Program to interface: List<T> not ArrayList<T>
  • Choose implementation by access pattern and data needs
  • Prefer List.of() for small immutable collections
  • Don't mix mutable and unmodifiable references
  • Size collections when capacity is known
#java#collections