Skip to content
Java

The Java Collections Framework, Explained

By The EbookWale Team · Updated June 16, 2026 · 4 min read

The Java Collections framework provides List, Set, Map and Queue with ready-made implementations like ArrayList, HashMap and TreeSet. Here's what each does, when to use it, the Big O, and the interview gotchas.

The Java Collections framework is a standard library of data structures — List, Set, Map, and Queue — with ready-made implementations like ArrayList, HashMap, and TreeSet. Instead of writing your own dynamic arrays or hash tables, you pick the right interface and implementation, and choosing well is a core Java skill (and interview topic).

The trick is understanding the split between interfaces (the abstract type, like List) and implementations (the concrete class, like ArrayList) — a direct application of abstraction.

List Set Map ArrayList LinkedList HashSet TreeSet HashMap TreeMap
Pick the interface (List/Set/Map), then the implementation.

The big three interfaces

List<String> names = new ArrayList<>();   // ordered, allows duplicates
Set<String>  tags  = new HashSet<>();      // unique, unordered
Map<String, Integer> counts = new HashMap<>();  // key → value

Always declare the variable as the interface (List) and assign the implementation (ArrayList). That way you can swap implementations later without changing the rest of your code — programming to the interface, the OOP way.

List — ordered, indexed, duplicates allowed

List<String> todos = new ArrayList<>();
todos.add("write");
todos.get(0);          // "write" — O(1) random access
  • ArrayList — backed by a resizable array. Fast index access, the right default.
  • LinkedList — a doubly linked list. Fast add/remove at the ends, slow index access.

In practice, reach for ArrayList unless you’re constantly inserting/removing at the front.

Set — uniqueness, fast membership

Set<String> seen = new HashSet<>();
seen.add("a");
seen.add("a");          // ignored — already present
seen.contains("a");     // O(1) average
  • HashSet — O(1) average operations, no order.
  • LinkedHashSet — preserves insertion order.
  • TreeSet — keeps elements sorted, O(log n).

Use a Set to dedupe or to test membership fast — the same idea as a hash set in coding interviews.

Map — key → value, the workhorse

Map<String, Integer> ages = new HashMap<>();
ages.put("Ana", 30);
ages.getOrDefault("Ben", 0);   // safe default
ages.merge("Ana", 1, Integer::sum);  // increment a count
  • HashMap — O(1) average lookup, no order. The default.
  • LinkedHashMap — preserves insertion order.
  • TreeMap — keys sorted, O(log n), supports range queries.

Which one? Big O cheat sheet

NeedUseLookupNotes
Ordered list, index accessArrayListO(1) indexdefault list
Lots of end insert/removeLinkedList / ArrayDequeO(1) endsqueue/stack
Unique items, fast checkHashSetO(1)no order
Unique + sortedTreeSetO(log n)ordered
Key → value, fastHashMapO(1)default map
Key → value, sorted keysTreeMapO(log n)range queries

This table is most of what collections interviews test: pick the structure whose strengths match the problem.

Generics — type safety for free

The <String> in List<String> is a generic — it tells the compiler the collection holds only Strings, catching type errors before the program runs and removing casts.

List<String> names = new ArrayList<>();
// names.add(42);  →  compile error, caught early

Common mistakes

  • Choosing LinkedList by default. ArrayList wins for almost every real workload; LinkedList’s theoretical insert speed rarely beats ArrayList in practice due to cache effects.
  • Mutating a collection while iterating it with a for-each loop → ConcurrentModificationException. Use an Iterator’s remove() or removeIf().
  • Overriding equals() but not hashCode() on objects used as HashMap keys or HashSet members — they’ll “vanish” because the hash bucket is wrong.
  • Using a non-thread-safe HashMap across threads. Use ConcurrentHashMap for concurrent access.
⚠️ GOTCHA — Put a custom object in a HashSet or as a HashMap key without overriding both equals() and hashCode(), and lookups fail — Java uses the default identity hash, so an "equal" object won't be found. Override them as a pair.

Where this fits

The Collections framework is Phase 3 of our Java roadmap, sitting on the OOP four pillars (interfaces, generics) and feeding directly into streams and lambdas, which process collections declaratively.

Each collection is drawn out with its Big O and “when to reach for it” in Java in Three Months, the job-ready tier, with the basics introduced in Java in One Month. For concurrent collections, custom data structures, and performance tuning, Java for Staff Engineers goes deeper.

Know each collection’s strengths, and choosing the right one becomes automatic.

Frequently asked questions

What is the Java Collections framework?

It is a set of interfaces (List, Set, Map, Queue) and ready-made implementations (ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap) for storing and manipulating groups of objects. It gives you standard, reusable data structures with consistent methods instead of writing your own.

What is the difference between ArrayList and LinkedList?

ArrayList is backed by a resizable array: fast random access by index (O(1)) but slow insertion or removal in the middle (O(n) due to shifting). LinkedList is a doubly linked list: fast insertion and removal at the ends (O(1)) but slow index access (O(n)). In practice ArrayList is the right default for most uses.

When should I use a HashMap vs a TreeMap?

Use a HashMap for fast O(1) average lookup when you do not care about order. Use a TreeMap when you need keys kept in sorted order or range queries, accepting O(log n) operations. LinkedHashMap is a middle option that preserves insertion order.

What is the difference between a List, a Set, and a Map?

A List is an ordered collection that allows duplicates and indexed access. A Set is a collection of unique elements with no duplicates. A Map stores key-value pairs with unique keys. Choose a List for ordered sequences, a Set for uniqueness, and a Map for lookups by key.